From ff020d8d5ba71e720c7c033eb16736d1222f13fd Mon Sep 17 00:00:00 2001 From: Mohammad Muazam Date: Wed, 15 May 2024 10:51:11 +0530 Subject: [PATCH 1/4] feat: add support for new resources --- docs/squadcast.md | 34 +++- go.mod | 4 +- go.sum | 6 + providers/squadcast/deduplication_rules.go | 27 +++- providers/squadcast/deduplocation_rules_v2.go | 74 +++++++++ providers/squadcast/escalation_policy.go | 10 +- providers/squadcast/global_event_rules.go | 151 ++++++++++++++++++ providers/squadcast/routing_rules.go | 27 +++- providers/squadcast/routing_rules_v2.go | 74 +++++++++ providers/squadcast/runbook.go | 9 +- providers/squadcast/schedules_v2.go | 76 +++++++++ providers/squadcast/service.go | 9 +- providers/squadcast/slo.go | 9 +- providers/squadcast/squad.go | 9 +- providers/squadcast/squadcast_provider.go | 63 +++++--- providers/squadcast/squadcast_service.go | 61 +++++-- providers/squadcast/status_page.go | 51 ++++++ providers/squadcast/status_page_components.go | 64 ++++++++ providers/squadcast/status_page_groups.go | 64 ++++++++ providers/squadcast/suppression_rule_v2.go | 74 +++++++++ providers/squadcast/suppression_rules.go | 27 +++- providers/squadcast/tagging_rule_v2.go | 74 +++++++++ providers/squadcast/tagging_rules.go | 27 +++- providers/squadcast/team.go | 9 +- providers/squadcast/team_member.go | 10 +- providers/squadcast/team_roles.go | 9 +- providers/squadcast/user.go | 9 +- providers/squadcast/webform.go | 45 ++++++ 28 files changed, 1026 insertions(+), 80 deletions(-) create mode 100644 providers/squadcast/deduplocation_rules_v2.go create mode 100644 providers/squadcast/global_event_rules.go create mode 100644 providers/squadcast/routing_rules_v2.go create mode 100644 providers/squadcast/schedules_v2.go create mode 100644 providers/squadcast/status_page.go create mode 100644 providers/squadcast/status_page_components.go create mode 100644 providers/squadcast/status_page_groups.go create mode 100644 providers/squadcast/suppression_rule_v2.go create mode 100644 providers/squadcast/tagging_rule_v2.go create mode 100644 providers/squadcast/webform.go diff --git a/docs/squadcast.md b/docs/squadcast.md index d1dd6fd482..8ff517a473 100644 --- a/docs/squadcast.md +++ b/docs/squadcast.md @@ -4,11 +4,10 @@ Syntax: `export SQUADCAST_REFRESH_TOKEN=` -OR +OR Add `--refresh-token` flag in cmd - ``` terraformer import squadcast --resources= --region=SQUADCAST_REGION ``` @@ -35,6 +34,7 @@ terraformer import squadcast --resources=team --region=us --team-name="Default T - `Import Deduplication Rules Resource` (without `--service-name` flag) - Deduplication Rules for all the services under Default Team will be generated. + ``` terraformer import squadcast --resources=deduplication_rules --region=us --team-name="Default Team" ``` @@ -48,20 +48,21 @@ terraformer import squadcast --resources=deduplication_rules --region=us --team- ### In order to use terraform files: -- Update version and add source in `provider.tf` +- Update version and add source in `provider.tf` - Go to `/generated/squadcast///provider.tf` - Add `source = "SquadcastHub/squadcast"` to squadcast inside `required_providers` - Update `version` in `required_providers` by removing `.exe` (Windows users only) - Update `terraform_version` - `cd /generated/squadcast//` - `terraform state replace-provider -auto-approve "registry.terraform.io/-/squadcast" "SquadcastHub/squadcast"` - + ### Example: + ``` terraform { required_providers { squadcast = { - version = "~> 1.0.5" + version = "~> 2.0.1" source = "SquadcastHub/squadcast" } } @@ -71,6 +72,7 @@ terraform { ### Flags: - `--team-name` + - Required for the following resources: - deduplication_rules - escalation_policy @@ -84,18 +86,30 @@ terraform { - team_member - team_roles - user + - webform + - schedules_v2 + - global_event_rules + - status_pages + - status_page_components + - status_page_groups - `--region` + - Supported Values: - `us` - `eu` - `--service-name` (optional) + - Supported for the following resources: - deduplication_rules - routing_rules - suppression_rules - tagging_rules + - deduplication_rules_v2 + - routing_rules_v2 + - suppression_rules_v2 + - tagging_rules_v2 - If service name is not provided, resources for specified automation rule for all the service within the specified team will be generated. However it will only generate for a specific service when this flag is used. [see examples](squadcast.md:36) - `--refresh-token` (optional) @@ -105,15 +119,25 @@ terraform { ### Supported resources: - [`deduplication_rules`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/deduplication_rules) +- [`deduplication_rule_v2`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/deduplication_rule_v2) - [`escalation_policy`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/escalation_policy) - [`routing_rules`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/routing_rules) +- [`routing_rule_v2`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/routing_rule_v2) - [`runbook`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/runbook) - [`service`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/service) - [`slo`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/slo) - [`squad`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/squad) - [`suppression_rules`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/suppression_rules) +- [`suppression_rule_v2`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/suppression_rule_v2) - [`tagging_rules`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/tagging_rules) +- [`tagging_rule_v2`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/tagging_rule_v2) - [`team`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/team) - [`team_member`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/team_member) - [`team_roles`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/team_role) - [`user`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/user) +- [`webforms`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/webform) +- [`status_pages`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/status_page) +- [`status_page_components`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/status_page_component) +- [`status_page_groups`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/status_page_group) +- [`global_event_rules`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/ger) +- [`schedules_v2`](https://registry.terraform.io/providers/SquadcastHub/squadcast/latest/docs/resources/schedule_v2) diff --git a/go.mod b/go.mod index ac705d93b9..c0846bd073 100644 --- a/go.mod +++ b/go.mod @@ -257,7 +257,7 @@ require ( github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.1.0 // indirect github.com/google/jsonapi v1.0.0 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/googleapis/gax-go/v2 v2.12.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect @@ -389,11 +389,13 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect + nhooyr.io/websocket v1.8.10 // indirect sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect ) require ( github.com/gofrs/uuid/v3 v3.1.2 + github.com/hasura/go-graphql-client v0.12.1 github.com/ionos-cloud/sdk-go-cert-manager v1.0.0 github.com/ionos-cloud/sdk-go-container-registry v1.0.0 github.com/ionos-cloud/sdk-go-dataplatform v1.0.1 diff --git a/go.sum b/go.sum index 31eccc180f..d4f6825081 100644 --- a/go.sum +++ b/go.sum @@ -895,6 +895,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg= @@ -1036,6 +1038,8 @@ github.com/hashicorp/vault v0.10.4/go.mod h1:KfSyffbKxoVyspOdlaGVjIuwLobi07qD1bA github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 h1:xixZ2bWeofWV68J+x6AzmKuVM/JWCQwkWm6GW/MUR6I= github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= +github.com/hasura/go-graphql-client v0.12.1 h1:tL+BCoyubkYYyaQ+tJz+oPe/pSxYwOJHwe5SSqqi6WI= +github.com/hasura/go-graphql-client v0.12.1/go.mod h1:F4N4kR6vY8amio3gEu3tjSZr8GPOXJr3zj72DKixfLE= github.com/heimweh/go-pagerduty v0.0.0-20210930203304-530eff2acdc6 h1:/D0VtHEOCdotE1vSB9XznceAjIGkUieZ4BF6VKUIqNU= github.com/heimweh/go-pagerduty v0.0.0-20210930203304-530eff2acdc6/go.mod h1:JtJGtgN0y9KOCaqFMZFaBCWskpO/KK3Ro9TwjP9ss6w= github.com/heroku/heroku-go/v5 v5.4.1 h1:J1nNnR3/571b2iUDHl+y1B3VkhKm6YOXIq9GGzQUv5s= @@ -2416,6 +2420,8 @@ k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42/go.mod h1:Z/45zLw8lUo4wdi k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 h1:HNSDgDCrr/6Ly3WEGKZftiE7IY19Vz2GdbOCyI4qqhc= k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= +nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q= +nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= diff --git a/providers/squadcast/deduplication_rules.go b/providers/squadcast/deduplication_rules.go index ea9b75efb8..a6e3ee86bb 100644 --- a/providers/squadcast/deduplication_rules.go +++ b/providers/squadcast/deduplication_rules.go @@ -41,15 +41,25 @@ func (g *DeduplicationRulesGenerator) createResources(deduplicationRules Dedupli func (g *DeduplicationRulesGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { - getServicesURL := "/v3/services" - responseService, err := Request[[]Service](getServicesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) if err != nil { return err } for _, service := range *responseService { - getDeduplicationRulesURL := fmt.Sprintf("/v3/services/%s/deduplication-rules", service.ID) - response, err := Request[DeduplicationRules](getDeduplicationRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/deduplication-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[DeduplicationRules](req) if err != nil { return err } @@ -57,8 +67,13 @@ func (g *DeduplicationRulesGenerator) InitResources() error { g.Resources = append(g.Resources, g.createResources(*response)...) } } else { - getDeduplicationRulesURL := fmt.Sprintf("/v3/services/%s/deduplication-rules", g.Args["service_id"]) - response, err := Request[DeduplicationRules](getDeduplicationRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/deduplication-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[DeduplicationRules](req) if err != nil { return err } diff --git a/providers/squadcast/deduplocation_rules_v2.go b/providers/squadcast/deduplocation_rules_v2.go new file mode 100644 index 0000000000..4f514f74d9 --- /dev/null +++ b/providers/squadcast/deduplocation_rules_v2.go @@ -0,0 +1,74 @@ +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type DeduplicationRuleGenerator struct { + SCService +} + +func (g *DeduplicationRuleGenerator) createResources(deduplicationRulesV2 DeduplicationRules) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, rule := range deduplicationRulesV2.Rules { + resourceList = append(resourceList, terraformutils.NewResource( + rule.ID, + fmt.Sprintf("deduplication_rule_v2_%s", rule.ID), + "squadcast_deduplication_rule_v2", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + "service_id": deduplicationRulesV2.ServiceID, + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *DeduplicationRuleGenerator) InitResources() error { + if len(g.Args["service_name"].(string)) == 0 { + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) + if err != nil { + return err + } + + for _, service := range *responseService { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/deduplication-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[DeduplicationRules](req) + if err != nil { + return err + } + + g.Resources = append(g.Resources, g.createResources(*response)...) + } + } else { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/deduplication-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[DeduplicationRules](req) + if err != nil { + return err + } + + g.Resources = g.createResources(*response) + } + return nil +} diff --git a/providers/squadcast/escalation_policy.go b/providers/squadcast/escalation_policy.go index d5ca56a116..157ac91694 100644 --- a/providers/squadcast/escalation_policy.go +++ b/providers/squadcast/escalation_policy.go @@ -34,8 +34,14 @@ func (g *EscalationPolicyGenerator) createResources(policies []EscalationPolicy) } func (g *EscalationPolicyGenerator) InitResources() error { - getEscalationPolicyURL := fmt.Sprintf("/v3/escalation-policies?owner_id=%s", g.Args["team_id"].(string)) - response, err := Request[[]EscalationPolicy](getEscalationPolicyURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/escalation-policies?owner_id=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + + response, err := Request[[]EscalationPolicy](req) if err != nil { return err } diff --git a/providers/squadcast/global_event_rules.go b/providers/squadcast/global_event_rules.go new file mode 100644 index 0000000000..0b94eda140 --- /dev/null +++ b/providers/squadcast/global_event_rules.go @@ -0,0 +1,151 @@ +package squadcast + +import ( + "fmt" + "log" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type GlobalEventRulesGenerator struct { + SCService +} + +type GER struct { + ID uint `json:"id"` + Rulesets []GER_Ruleset `json:"rulesets"` +} +type GER_Ruleset struct { + ID uint `json:"id"` + AlertSourceName string + AlertSourceShortName string `json:"alert_source_shortname"` + AlertSourceVersion string `json:"alert_source_version"` + + Rules []GER_Ruleset_Rules +} +type GER_Ruleset_Rules struct { + ID uint `json:"id"` + GER_ID uint `json:"global_event_rule_id"` +} + +func (g *GlobalEventRulesGenerator) createResources(ger []GER) []terraformutils.Resource { + var resourceList []terraformutils.Resource + alertSourcesMap, err := g.getAlertSources() + if err != nil { + log.Fatal(err) + } + for _, rule := range ger { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", rule.ID), + fmt.Sprintf("ger_%d", rule.ID), + "squadcast_ger", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + }, + []string{}, + map[string]interface{}{}, + )) + for _, rs := range rule.Rulesets { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", rs.ID), + fmt.Sprintf("ger_ruleset_%d", rs.ID), + "squadcast_ger_ruleset", + g.GetProviderName(), + map[string]string{ + "ger_id": fmt.Sprintf("%d", rule.ID), + "alert_source": alertSourcesMap[rs.AlertSourceShortName], + "alert_source_shortname": rs.AlertSourceShortName, + "alert_source_version": rs.AlertSourceVersion, + }, + []string{}, + map[string]interface{}{}, + )) + + for _, r := range rs.Rules { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", r.ID), + fmt.Sprintf("ger_ruleset_rule_%d", r.ID), + "squadcast_ger_ruleset_rule", + g.GetProviderName(), + map[string]string{ + "ger_id": fmt.Sprintf("%d", rule.ID), + "alert_source": alertSourcesMap[rs.AlertSourceShortName], + "alert_source_shortname": rs.AlertSourceShortName, + "alert_source_version": rs.AlertSourceVersion, + }, + []string{}, + map[string]interface{}{}, + )) + } + } + } + return resourceList +} + +func (g *GlobalEventRulesGenerator) InitResources() error { + req := TRequest{ + URL: fmt.Sprintf("/v3/global-event-rules?owner_id=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]GER](req) + if err != nil { + return err + } + + ger := *response + + for i, rule := range ger { + for j, rs := range rule.Rulesets { + req := TRequest{ + URL: fmt.Sprintf("/v3/global-event-rules/%d/rulesets/%s/%s/rules", rule.ID, rs.AlertSourceVersion, rs.AlertSourceShortName), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + resp, err := Request[[]GER_Ruleset_Rules](req) + if err != nil { + return err + } + ger[i].Rulesets[j].Rules = *resp + } + } + + g.Resources = g.createResources(*response) + return nil +} + +type AlertSource struct { + ID string `json:"_id"` + Type string `json:"type"` + Heading string `json:"heading"` + SupportDocURL string `json:"supportDoc"` + DisplayKeyOnly bool `json:"displayKeyOnly"` + ShortName string `json:"shortName"` + Version string `json:"version"` + + IsValid bool `json:"isValid"` + IsPrivate bool `json:"isPrivate"` + IsDeprecated bool `json:"deprecated"` +} + +func (g *GlobalEventRulesGenerator) getAlertSources() (map[string]string, error) { + alertSourcesMap := make(map[string]string, 0) + req := TRequest{ + URL: "/v2/public/integrations", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + IsV2: true, + } + alertSources, err := Request[[]AlertSource](req) + if err != nil { + return nil, err + } + for _, alertSourceData := range *alertSources { + alertSourcesMap[alertSourceData.ShortName] = alertSourceData.Type + } + return alertSourcesMap, nil +} diff --git a/providers/squadcast/routing_rules.go b/providers/squadcast/routing_rules.go index 2f0815d09d..4412de2d05 100644 --- a/providers/squadcast/routing_rules.go +++ b/providers/squadcast/routing_rules.go @@ -41,15 +41,25 @@ func (g *RoutingRulesGenerator) createResources(routingRules RoutingRules) []ter func (g *RoutingRulesGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { - getServicesURL := "/v3/services" - responseService, err := Request[[]Service](getServicesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) if err != nil { return err } for _, service := range *responseService { - getRoutingRulesURL := fmt.Sprintf("/v3/services/%s/routing-rules", service.ID) - response, err := Request[RoutingRules](getRoutingRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/routing-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[RoutingRules](req) if err != nil { return err } @@ -57,8 +67,13 @@ func (g *RoutingRulesGenerator) InitResources() error { g.Resources = append(g.Resources, g.createResources(*response)...) } } else { - getRoutingRulesURL := fmt.Sprintf("/v3/services/%s/routing-rules", g.Args["service_id"]) - response, err := Request[RoutingRules](getRoutingRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/routing-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[RoutingRules](req) if err != nil { return err } diff --git a/providers/squadcast/routing_rules_v2.go b/providers/squadcast/routing_rules_v2.go new file mode 100644 index 0000000000..78cf3675a9 --- /dev/null +++ b/providers/squadcast/routing_rules_v2.go @@ -0,0 +1,74 @@ +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type RoutingRuleGenerator struct { + SCService +} + +func (g *RoutingRuleGenerator) createResources(routingRules RoutingRules) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, rule := range routingRules.Rules { + resourceList = append(resourceList, terraformutils.NewResource( + rule.ID, + fmt.Sprintf("routing_rule_v2_%s", rule.ID), + "squadcast_routing_rule_v2", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + "service_id": routingRules.ServiceID, + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *RoutingRuleGenerator) InitResources() error { + if len(g.Args["service_name"].(string)) == 0 { + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) + if err != nil { + return err + } + + for _, service := range *responseService { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/routing-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[RoutingRules](req) + if err != nil { + return err + } + + g.Resources = append(g.Resources, g.createResources(*response)...) + } + } else { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/routing-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[RoutingRules](req) + if err != nil { + return err + } + + g.Resources = g.createResources(*response) + } + return nil +} diff --git a/providers/squadcast/runbook.go b/providers/squadcast/runbook.go index 1cb62a7a55..67a63f65b3 100644 --- a/providers/squadcast/runbook.go +++ b/providers/squadcast/runbook.go @@ -36,8 +36,13 @@ func (g *RunbookGenerator) createResources(runbooks []Runbook) []terraformutils. } func (g *RunbookGenerator) InitResources() error { - getRunbooksURL := "/v3/runbooks" - response, err := Request[[]Runbook](getRunbooksURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/runbooks", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]Runbook](req) if err != nil { return err } diff --git a/providers/squadcast/schedules_v2.go b/providers/squadcast/schedules_v2.go new file mode 100644 index 0000000000..907d2b534c --- /dev/null +++ b/providers/squadcast/schedules_v2.go @@ -0,0 +1,76 @@ +// service resource is yet to be implemented + +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type SchedulesGenerator struct { + SCService +} + +type ScheduleQueryStruct struct { + Schedules []*Schedule `graphql:"schedules(filters: { teamID: $teamID })"` +} + +type Schedule struct { + ID int `graphql:"ID" json:"ID"` + Rotations []*Rotation `graphql:"rotations" json:"rotations"` +} + +type Rotation struct { + ID int `graphql:"ID" json:"id"` +} + +func (g *SchedulesGenerator) createResources(schedules []*Schedule) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, sch := range schedules { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", sch.ID), + fmt.Sprintf("schedule_v2_%d", sch.ID), + "squadcast_schedule_v2", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + }, + []string{}, + map[string]interface{}{}, + )) + } + for _, sch := range schedules { + for _, rot := range sch.Rotations { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", rot.ID), + fmt.Sprintf("rotation_v2_%d", rot.ID), + "squadcast_schedule_rotation_v2", + g.GetProviderName(), + map[string]string{ + "schedule_id": fmt.Sprintf("%d", sch.ID), + }, + []string{}, + map[string]interface{}{}, + )) + } + } + + return resourceList +} + +func (g *SchedulesGenerator) InitResources() error { + var payload ScheduleQueryStruct + + variables := map[string]interface{}{ + "teamID": g.Args["team_id"].(string), + } + + response, err := GraphQLRequest[ScheduleQueryStruct]("query", g.Args["access_token"].(string), g.Args["region"].(string), &payload, variables) + if err != nil { + return err + } + + g.Resources = g.createResources(response.Schedules) + return nil +} diff --git a/providers/squadcast/service.go b/providers/squadcast/service.go index 674980f3bd..ef80cf30a8 100644 --- a/providers/squadcast/service.go +++ b/providers/squadcast/service.go @@ -36,8 +36,13 @@ func (g *ServiceGenerator) createResources(services []Service) []terraformutils. } func (g *ServiceGenerator) InitResources() error { - getServicesURL := "/v3/services" - response, err := Request[[]Service](getServicesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]Service](req) if err != nil { return err } diff --git a/providers/squadcast/slo.go b/providers/squadcast/slo.go index 48de8e4ec7..fb4c81b8c0 100644 --- a/providers/squadcast/slo.go +++ b/providers/squadcast/slo.go @@ -40,8 +40,13 @@ func (g *SLOGenerator) createResources(slo []SLO) []terraformutils.Resource { } func (g *SLOGenerator) InitResources() error { - getSLOsURL := fmt.Sprintf("/v3/slo?owner_id=%s", g.Args["team_id"].(string)) - response, err := Request[getSLOsResponse](getSLOsURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/slo?owner_id=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[getSLOsResponse](req) if err != nil { return err } diff --git a/providers/squadcast/squad.go b/providers/squadcast/squad.go index f9e8aa81f3..1b6f5cbf48 100644 --- a/providers/squadcast/squad.go +++ b/providers/squadcast/squad.go @@ -35,8 +35,13 @@ func (g *SquadGenerator) createResources(squads []Squad) []terraformutils.Resour } func (g *SquadGenerator) InitResources() error { - getSquadsURL := fmt.Sprintf("/v3/squads?owner_id=%s", g.Args["team_id"].(string)) - response, err := Request[[]Squad](getSquadsURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/squads?owner_id=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]Squad](req) if err != nil { return err } diff --git a/providers/squadcast/squadcast_provider.go b/providers/squadcast/squadcast_provider.go index 70be28db00..2001f7fde9 100644 --- a/providers/squadcast/squadcast_provider.go +++ b/providers/squadcast/squadcast_provider.go @@ -122,25 +122,40 @@ func (p *SCProvider) GetName() string { func (p *SCProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { return map[string]terraformutils.ServiceGenerator{ - "user": &UserGenerator{}, - "service": &ServiceGenerator{}, - "squad": &SquadGenerator{}, - "team": &TeamGenerator{}, - "team_member": &TeamMemberGenerator{}, - "team_roles": &TeamRolesGenerator{}, - "escalation_policy": &EscalationPolicyGenerator{}, - "runbook": &RunbookGenerator{}, - "slo": &SLOGenerator{}, - "tagging_rules": &TaggingRulesGenerator{}, - "routing_rules": &RoutingRulesGenerator{}, - "deduplication_rules": &DeduplicationRulesGenerator{}, - "suppression_rules": &SuppressionRulesGenerator{}, + "user": &UserGenerator{}, + "service": &ServiceGenerator{}, + "squad": &SquadGenerator{}, + "team": &TeamGenerator{}, + "team_member": &TeamMemberGenerator{}, + "team_roles": &TeamRolesGenerator{}, + "escalation_policy": &EscalationPolicyGenerator{}, + "runbook": &RunbookGenerator{}, + "slo": &SLOGenerator{}, + "tagging_rules": &TaggingRulesGenerator{}, + "tagging_rule_v2": &TaggingRuleGenerator{}, + "routing_rules": &RoutingRulesGenerator{}, + "routing_rule_v2": &RoutingRuleGenerator{}, + "deduplication_rules": &DeduplicationRulesGenerator{}, + "deduplication_rule_v2": &DeduplicationRuleGenerator{}, + "suppression_rules": &SuppressionRulesGenerator{}, + "suppression_rule_v2": &SuppressionRuleGenerator{}, + "global_event_rules": &GlobalEventRulesGenerator{}, + "webforms": &WebformsGenerator{}, + "status_pages": &StatusPagesGenerator{}, + "status_page_components": &StatusPageComponentsGenerator{}, + "status_page_groups": &StatusPageGroupsGenerator{}, + "schedules_v2": &SchedulesGenerator{}, } } func (p *SCProvider) GetAccessToken() { - url := "/oauth/access-token" - response, err := Request[AccessToken](url, p.refreshToken, p.region, false) + req := TRequest{ + URL: "/oauth/access-token", + RefreshToken: p.refreshToken, + Region: p.region, + IsAuthenticated: false, + } + response, err := Request[AccessToken](req) if err != nil { log.Fatal(err) } @@ -148,8 +163,13 @@ func (p *SCProvider) GetAccessToken() { } func (p *SCProvider) GetTeamID() { - url := fmt.Sprintf("/v3/teams/by-name?name=%s", url.QueryEscape(p.teamName)) - response, err := Request[Team](url, p.accessToken, p.region, true) + req := TRequest{ + URL: fmt.Sprintf("/v3/teams/by-name?name=%s", url.QueryEscape(p.teamName)), + AccessToken: p.accessToken, + Region: p.region, + IsAuthenticated: true, + } + response, err := Request[Team](req) if err != nil { log.Fatal(err) } @@ -157,8 +177,13 @@ func (p *SCProvider) GetTeamID() { } func (p *SCProvider) GetServiceID() { - url := fmt.Sprintf("/v3/services/by-name?name=%s&owner_id=%s", url.QueryEscape(p.serviceName), p.teamID) - response, err := Request[Service](url, p.accessToken, p.region, true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/by-name?name=%s&owner_id=%s", url.QueryEscape(p.serviceName), p.teamID), + AccessToken: p.accessToken, + Region: p.region, + IsAuthenticated: true, + } + response, err := Request[Service](req) if err != nil { log.Fatal(err) } diff --git a/providers/squadcast/squadcast_service.go b/providers/squadcast/squadcast_service.go index 640ada6a19..41988fe3b7 100644 --- a/providers/squadcast/squadcast_service.go +++ b/providers/squadcast/squadcast_service.go @@ -8,6 +8,8 @@ import ( "log" "net/http" + "github.com/hasura/go-graphql-client" + "github.com/GoogleCloudPlatform/terraformer/terraformutils" ) @@ -19,7 +21,7 @@ const ( UserAgent = "terraformer-squadcast" ) -func GetHost(region string) string { +func getHost(region string) string { switch region { case "us": return "squadcast.com" @@ -32,23 +34,38 @@ func GetHost(region string) string { } } -func Request[TRes any](url string, token string, region string, isAuthenticated bool) (*TRes, error) { +type TRequest struct { + URL string + AccessToken string + RefreshToken string + Region string + IsAuthenticated bool + IsV2 bool +} + +func Request[TRes any](request TRequest) (*TRes, error) { ctx := context.Background() var URL string var req *http.Request var err error - host := GetHost(region) - if isAuthenticated { - URL = fmt.Sprintf("https://api.%s%s", host, url) - req, err = http.NewRequestWithContext(ctx, http.MethodGet, URL, nil) - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) + host := getHost(request.Region) + if request.IsAuthenticated { + if !request.IsV2 { + URL = fmt.Sprintf("https://api.%s%s", host, request.URL) + req, err = http.NewRequestWithContext(ctx, http.MethodGet, URL, nil) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", request.AccessToken)) + } else { + URL = fmt.Sprintf("https://platform-backend.%s%s", host, request.URL) + req, err = http.NewRequestWithContext(ctx, http.MethodGet, URL, nil) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", request.AccessToken)) + } } else { - URL = fmt.Sprintf("https://auth.%s%s", host, url) + URL = fmt.Sprintf("https://auth.%s%s", host, request.URL) req, err = http.NewRequestWithContext(ctx, http.MethodGet, URL, nil) - req.Header.Set("X-Refresh-Token", token) + req.Header.Set("X-Refresh-Token", request.RefreshToken) } if err != nil { - log.Fatal(err) + return nil, err } req.Header.Set("User-Agent", UserAgent) @@ -58,10 +75,10 @@ func Request[TRes any](url string, token string, region string, isAuthenticated Data *TRes `json:"data"` *Meta } - if err != nil { - log.Fatal(err) + return nil, err } + defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { @@ -80,9 +97,27 @@ func Request[TRes any](url string, token string, region string, isAuthenticated } if response.Meta != nil { if response.Meta.Meta.Status >= 400 { - return nil, fmt.Errorf("Error: %s", response.Meta.Meta.Message) + return nil, fmt.Errorf("error: %s", response.Meta.Meta.Message) } } return response.Data, nil } + +var gqlClient *graphql.Client + +func GraphQLRequest[TReq any](method string, token string, region string, payload *TReq, variables map[string]interface{}) (*TReq, error) { + graphQLURL := fmt.Sprintf("https://api.%s/v3/graphql", getHost(region)) + bearerToken := fmt.Sprintf("Bearer %s", token) + + if gqlClient == nil { + gqlClient = graphql.NewClient(graphQLURL, nil).WithRequestModifier(func(req *http.Request) { + req.Header.Set("Authorization", bearerToken) + }) + } + if err := gqlClient.WithDebug(false).Query(context.Background(), payload, variables); err != nil { + return nil, err + } + + return payload, nil +} diff --git a/providers/squadcast/status_page.go b/providers/squadcast/status_page.go new file mode 100644 index 0000000000..a2cd5c2697 --- /dev/null +++ b/providers/squadcast/status_page.go @@ -0,0 +1,51 @@ +// service resource is yet to be implemented + +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type StatusPagesGenerator struct { + SCService +} + +type StatusPage struct { + ID uint `json:"id"` +} + +func (g *StatusPagesGenerator) createResources(statusPages []StatusPage) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, sp := range statusPages { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", sp.ID), + fmt.Sprintf("status_page_%d", sp.ID), + "squadcast_status_page", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *StatusPagesGenerator) InitResources() error { + req := TRequest{ + URL: fmt.Sprintf("/v4/statuspages?teamID=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]StatusPage](req) + if err != nil { + return err + } + + g.Resources = g.createResources(*response) + return nil +} diff --git a/providers/squadcast/status_page_components.go b/providers/squadcast/status_page_components.go new file mode 100644 index 0000000000..11a2135afe --- /dev/null +++ b/providers/squadcast/status_page_components.go @@ -0,0 +1,64 @@ +// service resource is yet to be implemented + +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type StatusPageComponentsGenerator struct { + SCService +} + +type StatusPageComponent struct { + ID uint `json:"id"` + PageID uint `json:"pageID"` +} + +func (g *StatusPageComponentsGenerator) createResources(statusPageComponents []StatusPageComponent) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, spc := range statusPageComponents { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", spc.ID), + fmt.Sprintf("status_page_component_%d", spc.ID), + "squadcast_status_page_component", + g.GetProviderName(), + map[string]string{ + "status_page_id": fmt.Sprintf("%d", spc.PageID), + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *StatusPageComponentsGenerator) InitResources() error { + req := TRequest{ + URL: fmt.Sprintf("/v4/statuspages?teamID=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]StatusPage](req) + if err != nil { + return err + } + for _, sp := range *response { + req := TRequest{ + URL: fmt.Sprintf("/v4/statuspages/%d/components", sp.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]StatusPageComponent](req) + if err != nil { + return err + } + + g.Resources = append(g.Resources, g.createResources(*response)...) + } + return nil +} diff --git a/providers/squadcast/status_page_groups.go b/providers/squadcast/status_page_groups.go new file mode 100644 index 0000000000..dc4a9d150a --- /dev/null +++ b/providers/squadcast/status_page_groups.go @@ -0,0 +1,64 @@ +// service resource is yet to be implemented + +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type StatusPageGroupsGenerator struct { + SCService +} + +type StatusPageGroup struct { + ID uint `json:"id"` + PageID uint `json:"pageID"` +} + +func (g *StatusPageGroupsGenerator) createResources(statusPageGroups []StatusPageGroup) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, spc := range statusPageGroups { + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", spc.ID), + fmt.Sprintf("status_page_group_%d", spc.ID), + "squadcast_status_page_group", + g.GetProviderName(), + map[string]string{ + "status_page_id": fmt.Sprintf("%d", spc.PageID), + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *StatusPageGroupsGenerator) InitResources() error { + req := TRequest{ + URL: fmt.Sprintf("/v4/statuspages?teamID=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]StatusPage](req) + if err != nil { + return err + } + for _, sp := range *response { + req := TRequest{ + URL: fmt.Sprintf("/v4/statuspages/%d/groups", sp.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]StatusPageGroup](req) + if err != nil { + return err + } + + g.Resources = append(g.Resources, g.createResources(*response)...) + } + return nil +} diff --git a/providers/squadcast/suppression_rule_v2.go b/providers/squadcast/suppression_rule_v2.go new file mode 100644 index 0000000000..aa08d88585 --- /dev/null +++ b/providers/squadcast/suppression_rule_v2.go @@ -0,0 +1,74 @@ +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type SuppressionRuleGenerator struct { + SCService +} + +func (g *SuppressionRuleGenerator) createResources(suppressionRules SuppressionRules) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, rule := range suppressionRules.Rules { + resourceList = append(resourceList, terraformutils.NewResource( + rule.ID, + fmt.Sprintf("suppression_rule_v2_%s", rule.ID), + "squadcast_suppression_rule_v2", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + "service_id": suppressionRules.ServiceID, + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *SuppressionRuleGenerator) InitResources() error { + if len(g.Args["service_name"].(string)) == 0 { + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) + if err != nil { + return err + } + + for _, service := range *responseService { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/suppression-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[SuppressionRules](req) + if err != nil { + return err + } + + g.Resources = append(g.Resources, g.createResources(*response)...) + } + } else { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/suppression-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[SuppressionRules](req) + if err != nil { + return err + } + + g.Resources = g.createResources(*response) + } + return nil +} diff --git a/providers/squadcast/suppression_rules.go b/providers/squadcast/suppression_rules.go index 1fd937af97..c32efd65b0 100644 --- a/providers/squadcast/suppression_rules.go +++ b/providers/squadcast/suppression_rules.go @@ -41,15 +41,25 @@ func (g *SuppressionRulesGenerator) createResources(suppressionRules Suppression func (g *SuppressionRulesGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { - getServicesURL := "/v3/services" - responseService, err := Request[[]Service](getServicesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) if err != nil { return err } for _, service := range *responseService { - getSuppressionRulesURL := fmt.Sprintf("/v3/services/%s/suppression-rules", service.ID) - response, err := Request[SuppressionRules](getSuppressionRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/suppression-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[SuppressionRules](req) if err != nil { return err } @@ -57,8 +67,13 @@ func (g *SuppressionRulesGenerator) InitResources() error { g.Resources = append(g.Resources, g.createResources(*response)...) } } else { - getSuppressionRulesURL := fmt.Sprintf("/v3/services/%s/suppression-rules", g.Args["service_id"]) - response, err := Request[SuppressionRules](getSuppressionRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/suppression-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[SuppressionRules](req) if err != nil { return err } diff --git a/providers/squadcast/tagging_rule_v2.go b/providers/squadcast/tagging_rule_v2.go new file mode 100644 index 0000000000..4f3b7977b1 --- /dev/null +++ b/providers/squadcast/tagging_rule_v2.go @@ -0,0 +1,74 @@ +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type TaggingRuleGenerator struct { + SCService +} + +func (g *TaggingRuleGenerator) createResources(taggingRule TaggingRules) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, rule := range taggingRule.Rules { + resourceList = append(resourceList, terraformutils.NewResource( + rule.ID, + fmt.Sprintf("tagging_rule_v2_%s", rule.ID), + "squadcast_tagging_rule_v2", + g.GetProviderName(), + map[string]string{ + "team_id": g.Args["team_id"].(string), + "service_id": taggingRule.ServiceID, + }, + []string{}, + map[string]interface{}{}, + )) + } + return resourceList +} + +func (g *TaggingRuleGenerator) InitResources() error { + if len(g.Args["service_name"].(string)) == 0 { + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) + if err != nil { + return err + } + + for _, service := range *responseService { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/tagging-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[TaggingRules](req) + if err != nil { + return err + } + + g.Resources = append(g.Resources, g.createResources(*response)...) + } + } else { + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/tagging-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[TaggingRules](req) + if err != nil { + return err + } + + g.Resources = g.createResources(*response) + } + return nil +} diff --git a/providers/squadcast/tagging_rules.go b/providers/squadcast/tagging_rules.go index 22957ef2c3..aa5bea6c20 100644 --- a/providers/squadcast/tagging_rules.go +++ b/providers/squadcast/tagging_rules.go @@ -41,15 +41,25 @@ func (g *TaggingRulesGenerator) createResources(taggingRule TaggingRules) []terr func (g *TaggingRulesGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { - getServicesURL := "/v3/services" - responseService, err := Request[[]Service](getServicesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/services", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + responseService, err := Request[[]Service](req) if err != nil { return err } for _, service := range *responseService { - getTaggingRulesURL := fmt.Sprintf("/v3/services/%s/tagging-rules", service.ID) - response, err := Request[TaggingRules](getTaggingRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/tagging-rules", service.ID), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[TaggingRules](req) if err != nil { return err } @@ -57,8 +67,13 @@ func (g *TaggingRulesGenerator) InitResources() error { g.Resources = append(g.Resources, g.createResources(*response)...) } } else { - getTaggingRulesURL := fmt.Sprintf("/v3/services/%s/tagging-rules", g.Args["service_id"]) - response, err := Request[TaggingRules](getTaggingRulesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/services/%s/tagging-rules", g.Args["service_id"]), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[TaggingRules](req) if err != nil { return err } diff --git a/providers/squadcast/team.go b/providers/squadcast/team.go index be1728da74..ca23f906ad 100644 --- a/providers/squadcast/team.go +++ b/providers/squadcast/team.go @@ -32,8 +32,13 @@ func (g *TeamGenerator) createResources(teams []Team) []terraformutils.Resource } func (g *TeamGenerator) InitResources() error { - getTeamsURL := "/v3/teams" - response, err := Request[[]Team](getTeamsURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/teams", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]Team](req) if err != nil { return err } diff --git a/providers/squadcast/team_member.go b/providers/squadcast/team_member.go index af5e42cb30..088610522b 100644 --- a/providers/squadcast/team_member.go +++ b/providers/squadcast/team_member.go @@ -34,8 +34,14 @@ func (g *TeamMemberGenerator) createResources(team Team) []terraformutils.Resour } func (g *TeamMemberGenerator) InitResources() error { - getTeamURL := fmt.Sprintf("/v3/teams/by-name?name=%s", url.QueryEscape(g.Args["team_name"].(string))) - response, err := Request[Team](getTeamURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/teams/by-name?name=%s", url.QueryEscape(g.Args["team_name"].(string))), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + + response, err := Request[Team](req) if err != nil { return err } diff --git a/providers/squadcast/team_roles.go b/providers/squadcast/team_roles.go index 166b73e492..ec9f47a942 100644 --- a/providers/squadcast/team_roles.go +++ b/providers/squadcast/team_roles.go @@ -34,8 +34,13 @@ func (g *TeamRolesGenerator) createResources(teamRoles []TeamRole) []terraformut } func (g *TeamRolesGenerator) InitResources() error { - getTeamRolesURL := fmt.Sprintf("/v3/teams/%s/roles", g.Args["team_id"].(string)) - response, err := Request[[]TeamRole](getTeamRolesURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: fmt.Sprintf("/v3/teams/%s/roles", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]TeamRole](req) if err != nil { return err } diff --git a/providers/squadcast/user.go b/providers/squadcast/user.go index 52a842de1a..2b77b06f9d 100644 --- a/providers/squadcast/user.go +++ b/providers/squadcast/user.go @@ -29,8 +29,13 @@ func (g *UserGenerator) createResources(users []User) []terraformutils.Resource } func (g *UserGenerator) InitResources() error { - getUsersURL := "/v3/users" - response, err := Request[[]User](getUsersURL, g.Args["access_token"].(string), g.Args["region"].(string), true) + req := TRequest{ + URL: "/v3/users", + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]User](req) if err != nil { return err } diff --git a/providers/squadcast/webform.go b/providers/squadcast/webform.go new file mode 100644 index 0000000000..86f73c472c --- /dev/null +++ b/providers/squadcast/webform.go @@ -0,0 +1,45 @@ +package squadcast + +import ( + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type WebformsGenerator struct { + SCService +} + +type Webform struct { + ID uint `json:"id"` +} + +func (g *WebformsGenerator) createResources(webforms []Webform) []terraformutils.Resource { + var resourceList []terraformutils.Resource + for _, webform := range webforms { + resourceList = append(resourceList, terraformutils.NewSimpleResource( + fmt.Sprintf("%d", webform.ID), + fmt.Sprintf("webform_%d", webform.ID), + "squadcast_webform", + g.GetProviderName(), + []string{}, + )) + } + return resourceList +} + +func (g *WebformsGenerator) InitResources() error { + req := TRequest{ + URL: fmt.Sprintf("/v3/webforms?owner_id=%s", g.Args["team_id"].(string)), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, err := Request[[]Webform](req) + if err != nil { + return err + } + + g.Resources = g.createResources(*response) + return nil +} From c1aee7bf25792e8ddf1a1c7fe2fbf6a53f32707f Mon Sep 17 00:00:00 2001 From: Mohammad Muazam Date: Wed, 15 May 2024 12:12:56 +0530 Subject: [PATCH 2/4] minor change --- providers/squadcast/global_event_rules.go | 14 +++----------- providers/squadcast/schedules_v2.go | 3 +-- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/providers/squadcast/global_event_rules.go b/providers/squadcast/global_event_rules.go index 0b94eda140..c2ca80709b 100644 --- a/providers/squadcast/global_event_rules.go +++ b/providers/squadcast/global_event_rules.go @@ -118,17 +118,9 @@ func (g *GlobalEventRulesGenerator) InitResources() error { } type AlertSource struct { - ID string `json:"_id"` - Type string `json:"type"` - Heading string `json:"heading"` - SupportDocURL string `json:"supportDoc"` - DisplayKeyOnly bool `json:"displayKeyOnly"` - ShortName string `json:"shortName"` - Version string `json:"version"` - - IsValid bool `json:"isValid"` - IsPrivate bool `json:"isPrivate"` - IsDeprecated bool `json:"deprecated"` + Type string `json:"type"` + ShortName string `json:"shortName"` + Version string `json:"version"` } func (g *GlobalEventRulesGenerator) getAlertSources() (map[string]string, error) { diff --git a/providers/squadcast/schedules_v2.go b/providers/squadcast/schedules_v2.go index 907d2b534c..e474d06968 100644 --- a/providers/squadcast/schedules_v2.go +++ b/providers/squadcast/schedules_v2.go @@ -39,8 +39,7 @@ func (g *SchedulesGenerator) createResources(schedules []*Schedule) []terraformu []string{}, map[string]interface{}{}, )) - } - for _, sch := range schedules { + for _, rot := range sch.Rotations { resourceList = append(resourceList, terraformutils.NewResource( fmt.Sprintf("%d", rot.ID), From 2603fb6a2cca936c9db7c3e93b92a8ee0496cb55 Mon Sep 17 00:00:00 2001 From: Mohammad Muazam Date: Wed, 15 May 2024 16:15:16 +0530 Subject: [PATCH 3/4] implement pagination --- providers/squadcast/deduplication_rules.go | 6 +- providers/squadcast/deduplocation_rules_v2.go | 6 +- providers/squadcast/escalation_policy.go | 2 +- providers/squadcast/global_event_rules.go | 61 +++++++++++++------ providers/squadcast/routing_rules.go | 6 +- providers/squadcast/routing_rules_v2.go | 6 +- providers/squadcast/runbook.go | 2 +- providers/squadcast/service.go | 2 +- providers/squadcast/slo.go | 2 +- providers/squadcast/squad.go | 2 +- providers/squadcast/squadcast_provider.go | 11 ++-- providers/squadcast/squadcast_service.go | 16 ++--- providers/squadcast/status_page.go | 30 ++++++--- providers/squadcast/status_page_components.go | 4 +- providers/squadcast/status_page_groups.go | 4 +- providers/squadcast/suppression_rule_v2.go | 6 +- providers/squadcast/suppression_rules.go | 6 +- providers/squadcast/tagging_rule_v2.go | 6 +- providers/squadcast/tagging_rules.go | 6 +- providers/squadcast/team.go | 2 +- providers/squadcast/team_member.go | 2 +- providers/squadcast/team_roles.go | 2 +- providers/squadcast/user.go | 2 +- providers/squadcast/webform.go | 2 +- 24 files changed, 116 insertions(+), 78 deletions(-) diff --git a/providers/squadcast/deduplication_rules.go b/providers/squadcast/deduplication_rules.go index a6e3ee86bb..0eeb239c00 100644 --- a/providers/squadcast/deduplication_rules.go +++ b/providers/squadcast/deduplication_rules.go @@ -47,7 +47,7 @@ func (g *DeduplicationRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -59,7 +59,7 @@ func (g *DeduplicationRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[DeduplicationRules](req) + response, _, err := Request[DeduplicationRules](req) if err != nil { return err } @@ -73,7 +73,7 @@ func (g *DeduplicationRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[DeduplicationRules](req) + response, _, err := Request[DeduplicationRules](req) if err != nil { return err } diff --git a/providers/squadcast/deduplocation_rules_v2.go b/providers/squadcast/deduplocation_rules_v2.go index 4f514f74d9..fd38f19c6c 100644 --- a/providers/squadcast/deduplocation_rules_v2.go +++ b/providers/squadcast/deduplocation_rules_v2.go @@ -37,7 +37,7 @@ func (g *DeduplicationRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -49,7 +49,7 @@ func (g *DeduplicationRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[DeduplicationRules](req) + response, _, err := Request[DeduplicationRules](req) if err != nil { return err } @@ -63,7 +63,7 @@ func (g *DeduplicationRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[DeduplicationRules](req) + response, _, err := Request[DeduplicationRules](req) if err != nil { return err } diff --git a/providers/squadcast/escalation_policy.go b/providers/squadcast/escalation_policy.go index 157ac91694..55a62d7475 100644 --- a/providers/squadcast/escalation_policy.go +++ b/providers/squadcast/escalation_policy.go @@ -41,7 +41,7 @@ func (g *EscalationPolicyGenerator) InitResources() error { IsAuthenticated: true, } - response, err := Request[[]EscalationPolicy](req) + response, _, err := Request[[]EscalationPolicy](req) if err != nil { return err } diff --git a/providers/squadcast/global_event_rules.go b/providers/squadcast/global_event_rules.go index c2ca80709b..9d6c79aa05 100644 --- a/providers/squadcast/global_event_rules.go +++ b/providers/squadcast/global_event_rules.go @@ -77,43 +77,70 @@ func (g *GlobalEventRulesGenerator) createResources(ger []GER) []terraformutils. []string{}, map[string]interface{}{}, )) + } + resourceList = append(resourceList, terraformutils.NewResource( + fmt.Sprintf("%d", rs.ID), + fmt.Sprintf("ger_ruleset_rules_ordering_%d", rs.ID), + "squadcast_ger_ruleset_rules_ordering", + g.GetProviderName(), + map[string]string{ + "ger_id": fmt.Sprintf("%d", rule.ID), + "alert_source": alertSourcesMap[rs.AlertSourceShortName], + "alert_source_shortname": rs.AlertSourceShortName, + "alert_source_version": rs.AlertSourceVersion, + }, + []string{}, + map[string]interface{}{}, + )) } } return resourceList } func (g *GlobalEventRulesGenerator) InitResources() error { - req := TRequest{ - URL: fmt.Sprintf("/v3/global-event-rules?owner_id=%s", g.Args["team_id"].(string)), - AccessToken: g.Args["access_token"].(string), - Region: g.Args["region"].(string), - IsAuthenticated: true, - } - response, err := Request[[]GER](req) - if err != nil { - return err - } + var allRules []GER + page := 1 + pageSize := 100 - ger := *response + for { + req := TRequest{ + URL: fmt.Sprintf("/v3/global-event-rules?owner_id=%s&page_number=%d&page_size=%d", g.Args["team_id"].(string), page, pageSize), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + + response, meta, err := Request[[]GER](req) + if err != nil { + return err + } + + allRules = append(allRules, *response...) + if page*pageSize >= meta.TotalCount { + break + } + + page++ + } - for i, rule := range ger { + for i, rule := range allRules { for j, rs := range rule.Rulesets { req := TRequest{ - URL: fmt.Sprintf("/v3/global-event-rules/%d/rulesets/%s/%s/rules", rule.ID, rs.AlertSourceVersion, rs.AlertSourceShortName), + URL: fmt.Sprintf("/v3/global-event-rules/%d/rulesets/%s/%s/rules?page_number=1&page_size=100", rule.ID, rs.AlertSourceVersion, rs.AlertSourceShortName), AccessToken: g.Args["access_token"].(string), Region: g.Args["region"].(string), IsAuthenticated: true, } - resp, err := Request[[]GER_Ruleset_Rules](req) + resp, _, err := Request[[]GER_Ruleset_Rules](req) if err != nil { return err } - ger[i].Rulesets[j].Rules = *resp + allRules[i].Rulesets[j].Rules = *resp } } - g.Resources = g.createResources(*response) + g.Resources = g.createResources(allRules) return nil } @@ -132,7 +159,7 @@ func (g *GlobalEventRulesGenerator) getAlertSources() (map[string]string, error) IsAuthenticated: true, IsV2: true, } - alertSources, err := Request[[]AlertSource](req) + alertSources, _, err := Request[[]AlertSource](req) if err != nil { return nil, err } diff --git a/providers/squadcast/routing_rules.go b/providers/squadcast/routing_rules.go index 4412de2d05..8ac4f6dd18 100644 --- a/providers/squadcast/routing_rules.go +++ b/providers/squadcast/routing_rules.go @@ -47,7 +47,7 @@ func (g *RoutingRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -59,7 +59,7 @@ func (g *RoutingRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[RoutingRules](req) + response, _, err := Request[RoutingRules](req) if err != nil { return err } @@ -73,7 +73,7 @@ func (g *RoutingRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[RoutingRules](req) + response, _, err := Request[RoutingRules](req) if err != nil { return err } diff --git a/providers/squadcast/routing_rules_v2.go b/providers/squadcast/routing_rules_v2.go index 78cf3675a9..eb9964c6d5 100644 --- a/providers/squadcast/routing_rules_v2.go +++ b/providers/squadcast/routing_rules_v2.go @@ -37,7 +37,7 @@ func (g *RoutingRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -49,7 +49,7 @@ func (g *RoutingRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[RoutingRules](req) + response, _, err := Request[RoutingRules](req) if err != nil { return err } @@ -63,7 +63,7 @@ func (g *RoutingRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[RoutingRules](req) + response, _, err := Request[RoutingRules](req) if err != nil { return err } diff --git a/providers/squadcast/runbook.go b/providers/squadcast/runbook.go index 67a63f65b3..ab68d9eb1e 100644 --- a/providers/squadcast/runbook.go +++ b/providers/squadcast/runbook.go @@ -42,7 +42,7 @@ func (g *RunbookGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]Runbook](req) + response, _, err := Request[[]Runbook](req) if err != nil { return err } diff --git a/providers/squadcast/service.go b/providers/squadcast/service.go index ef80cf30a8..4bb31276dc 100644 --- a/providers/squadcast/service.go +++ b/providers/squadcast/service.go @@ -42,7 +42,7 @@ func (g *ServiceGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]Service](req) + response, _, err := Request[[]Service](req) if err != nil { return err } diff --git a/providers/squadcast/slo.go b/providers/squadcast/slo.go index fb4c81b8c0..c67b6a578c 100644 --- a/providers/squadcast/slo.go +++ b/providers/squadcast/slo.go @@ -46,7 +46,7 @@ func (g *SLOGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[getSLOsResponse](req) + response, _, err := Request[getSLOsResponse](req) if err != nil { return err } diff --git a/providers/squadcast/squad.go b/providers/squadcast/squad.go index 1b6f5cbf48..6cdf5059e8 100644 --- a/providers/squadcast/squad.go +++ b/providers/squadcast/squad.go @@ -41,7 +41,7 @@ func (g *SquadGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]Squad](req) + response, _, err := Request[[]Squad](req) if err != nil { return err } diff --git a/providers/squadcast/squadcast_provider.go b/providers/squadcast/squadcast_provider.go index 2001f7fde9..a6e27d8d2e 100644 --- a/providers/squadcast/squadcast_provider.go +++ b/providers/squadcast/squadcast_provider.go @@ -29,7 +29,9 @@ type AccessToken struct { // Meta holds the status of the request information type Meta struct { - Meta AppError `json:"meta,omitempty"` + Meta AppError `json:"meta,omitempty"` + TotalCount int `json:"total_count"` + TotalPages int `json:"totalCount"` } type AppError struct { @@ -38,7 +40,6 @@ type AppError struct { } func (p *SCProvider) Init(args []string) error { - if refreshToken := os.Getenv("SQUADCAST_REFRESH_TOKEN"); refreshToken != "" { p.refreshToken = os.Getenv("SQUADCAST_REFRESH_TOKEN") } @@ -155,7 +156,7 @@ func (p *SCProvider) GetAccessToken() { Region: p.region, IsAuthenticated: false, } - response, err := Request[AccessToken](req) + response, _, err := Request[AccessToken](req) if err != nil { log.Fatal(err) } @@ -169,7 +170,7 @@ func (p *SCProvider) GetTeamID() { Region: p.region, IsAuthenticated: true, } - response, err := Request[Team](req) + response, _, err := Request[Team](req) if err != nil { log.Fatal(err) } @@ -183,7 +184,7 @@ func (p *SCProvider) GetServiceID() { Region: p.region, IsAuthenticated: true, } - response, err := Request[Service](req) + response, _, err := Request[Service](req) if err != nil { log.Fatal(err) } diff --git a/providers/squadcast/squadcast_service.go b/providers/squadcast/squadcast_service.go index 41988fe3b7..2fef0e30b5 100644 --- a/providers/squadcast/squadcast_service.go +++ b/providers/squadcast/squadcast_service.go @@ -43,7 +43,7 @@ type TRequest struct { IsV2 bool } -func Request[TRes any](request TRequest) (*TRes, error) { +func Request[TRes any](request TRequest) (*TRes, *Meta, error) { ctx := context.Background() var URL string var req *http.Request @@ -65,7 +65,7 @@ func Request[TRes any](request TRequest) (*TRes, error) { req.Header.Set("X-Refresh-Token", request.RefreshToken) } if err != nil { - return nil, err + return nil, nil, err } req.Header.Set("User-Agent", UserAgent) @@ -73,10 +73,10 @@ func Request[TRes any](request TRequest) (*TRes, error) { var response struct { Data *TRes `json:"data"` - *Meta + Meta *Meta `json:"meta"` } if err != nil { - return nil, err + return nil, nil, err } defer func(Body io.ReadCloser) { @@ -88,20 +88,20 @@ func Request[TRes any](request TRequest) (*TRes, error) { body, err := io.ReadAll(resp.Body) if err != nil { - return nil, err + return nil, nil, err } err = json.Unmarshal(body, &response) if err != nil { - return nil, err + return nil, nil, err } if response.Meta != nil { if response.Meta.Meta.Status >= 400 { - return nil, fmt.Errorf("error: %s", response.Meta.Meta.Message) + return nil, nil, fmt.Errorf("error: %s", response.Meta.Meta.Message) } } - return response.Data, nil + return response.Data, response.Meta, nil } var gqlClient *graphql.Client diff --git a/providers/squadcast/status_page.go b/providers/squadcast/status_page.go index a2cd5c2697..543c06e74d 100644 --- a/providers/squadcast/status_page.go +++ b/providers/squadcast/status_page.go @@ -35,17 +35,27 @@ func (g *StatusPagesGenerator) createResources(statusPages []StatusPage) []terra } func (g *StatusPagesGenerator) InitResources() error { - req := TRequest{ - URL: fmt.Sprintf("/v4/statuspages?teamID=%s", g.Args["team_id"].(string)), - AccessToken: g.Args["access_token"].(string), - Region: g.Args["region"].(string), - IsAuthenticated: true, - } - response, err := Request[[]StatusPage](req) - if err != nil { - return err + var allStatusPages []StatusPage + page := 1 + pageSize := 100 + for { + req := TRequest{ + URL: fmt.Sprintf("/v4/statuspages?teamID=%s&pageNumber=%d&pageSize=%d", g.Args["team_id"].(string), page, pageSize), + AccessToken: g.Args["access_token"].(string), + Region: g.Args["region"].(string), + IsAuthenticated: true, + } + response, meta, err := Request[[]StatusPage](req) + if err != nil { + return err + } + allStatusPages = append(allStatusPages, *response...) + if page*pageSize >= meta.TotalPages { + break + } + page++ } - g.Resources = g.createResources(*response) + g.Resources = g.createResources(allStatusPages) return nil } diff --git a/providers/squadcast/status_page_components.go b/providers/squadcast/status_page_components.go index 11a2135afe..b8b2d24571 100644 --- a/providers/squadcast/status_page_components.go +++ b/providers/squadcast/status_page_components.go @@ -42,7 +42,7 @@ func (g *StatusPageComponentsGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]StatusPage](req) + response, _, err := Request[[]StatusPage](req) if err != nil { return err } @@ -53,7 +53,7 @@ func (g *StatusPageComponentsGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]StatusPageComponent](req) + response, _, err := Request[[]StatusPageComponent](req) if err != nil { return err } diff --git a/providers/squadcast/status_page_groups.go b/providers/squadcast/status_page_groups.go index dc4a9d150a..d35fa1bcbb 100644 --- a/providers/squadcast/status_page_groups.go +++ b/providers/squadcast/status_page_groups.go @@ -42,7 +42,7 @@ func (g *StatusPageGroupsGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]StatusPage](req) + response, _, err := Request[[]StatusPage](req) if err != nil { return err } @@ -53,7 +53,7 @@ func (g *StatusPageGroupsGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]StatusPageGroup](req) + response, _, err := Request[[]StatusPageGroup](req) if err != nil { return err } diff --git a/providers/squadcast/suppression_rule_v2.go b/providers/squadcast/suppression_rule_v2.go index aa08d88585..279850077a 100644 --- a/providers/squadcast/suppression_rule_v2.go +++ b/providers/squadcast/suppression_rule_v2.go @@ -37,7 +37,7 @@ func (g *SuppressionRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -49,7 +49,7 @@ func (g *SuppressionRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[SuppressionRules](req) + response, _, err := Request[SuppressionRules](req) if err != nil { return err } @@ -63,7 +63,7 @@ func (g *SuppressionRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[SuppressionRules](req) + response, _, err := Request[SuppressionRules](req) if err != nil { return err } diff --git a/providers/squadcast/suppression_rules.go b/providers/squadcast/suppression_rules.go index c32efd65b0..ea3021e159 100644 --- a/providers/squadcast/suppression_rules.go +++ b/providers/squadcast/suppression_rules.go @@ -47,7 +47,7 @@ func (g *SuppressionRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -59,7 +59,7 @@ func (g *SuppressionRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[SuppressionRules](req) + response, _, err := Request[SuppressionRules](req) if err != nil { return err } @@ -73,7 +73,7 @@ func (g *SuppressionRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[SuppressionRules](req) + response, _, err := Request[SuppressionRules](req) if err != nil { return err } diff --git a/providers/squadcast/tagging_rule_v2.go b/providers/squadcast/tagging_rule_v2.go index 4f3b7977b1..aee7a72dad 100644 --- a/providers/squadcast/tagging_rule_v2.go +++ b/providers/squadcast/tagging_rule_v2.go @@ -37,7 +37,7 @@ func (g *TaggingRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -49,7 +49,7 @@ func (g *TaggingRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[TaggingRules](req) + response, _, err := Request[TaggingRules](req) if err != nil { return err } @@ -63,7 +63,7 @@ func (g *TaggingRuleGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[TaggingRules](req) + response, _, err := Request[TaggingRules](req) if err != nil { return err } diff --git a/providers/squadcast/tagging_rules.go b/providers/squadcast/tagging_rules.go index aa5bea6c20..9413d2442c 100644 --- a/providers/squadcast/tagging_rules.go +++ b/providers/squadcast/tagging_rules.go @@ -47,7 +47,7 @@ func (g *TaggingRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - responseService, err := Request[[]Service](req) + responseService, _, err := Request[[]Service](req) if err != nil { return err } @@ -59,7 +59,7 @@ func (g *TaggingRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[TaggingRules](req) + response, _, err := Request[TaggingRules](req) if err != nil { return err } @@ -73,7 +73,7 @@ func (g *TaggingRulesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[TaggingRules](req) + response, _, err := Request[TaggingRules](req) if err != nil { return err } diff --git a/providers/squadcast/team.go b/providers/squadcast/team.go index ca23f906ad..ceb8f349ac 100644 --- a/providers/squadcast/team.go +++ b/providers/squadcast/team.go @@ -38,7 +38,7 @@ func (g *TeamGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]Team](req) + response, _, err := Request[[]Team](req) if err != nil { return err } diff --git a/providers/squadcast/team_member.go b/providers/squadcast/team_member.go index 088610522b..a26ef4ba07 100644 --- a/providers/squadcast/team_member.go +++ b/providers/squadcast/team_member.go @@ -41,7 +41,7 @@ func (g *TeamMemberGenerator) InitResources() error { IsAuthenticated: true, } - response, err := Request[Team](req) + response, _, err := Request[Team](req) if err != nil { return err } diff --git a/providers/squadcast/team_roles.go b/providers/squadcast/team_roles.go index ec9f47a942..1660bef65a 100644 --- a/providers/squadcast/team_roles.go +++ b/providers/squadcast/team_roles.go @@ -40,7 +40,7 @@ func (g *TeamRolesGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]TeamRole](req) + response, _, err := Request[[]TeamRole](req) if err != nil { return err } diff --git a/providers/squadcast/user.go b/providers/squadcast/user.go index 2b77b06f9d..f6e337ab13 100644 --- a/providers/squadcast/user.go +++ b/providers/squadcast/user.go @@ -35,7 +35,7 @@ func (g *UserGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]User](req) + response, _, err := Request[[]User](req) if err != nil { return err } diff --git a/providers/squadcast/webform.go b/providers/squadcast/webform.go index 86f73c472c..9f5ba7449f 100644 --- a/providers/squadcast/webform.go +++ b/providers/squadcast/webform.go @@ -35,7 +35,7 @@ func (g *WebformsGenerator) InitResources() error { Region: g.Args["region"].(string), IsAuthenticated: true, } - response, err := Request[[]Webform](req) + response, _, err := Request[[]Webform](req) if err != nil { return err } From 2433691d74ce2f53f6b5a7c2e8612347e2c621af Mon Sep 17 00:00:00 2001 From: Mohammad Muazam Date: Fri, 17 May 2024 15:56:24 +0530 Subject: [PATCH 4/4] update services url --- providers/squadcast/deduplocation_rules_v2.go | 7 ++++++- providers/squadcast/routing_rules_v2.go | 7 ++++++- providers/squadcast/service.go | 7 ++++++- providers/squadcast/suppression_rule_v2.go | 7 ++++++- providers/squadcast/tagging_rule_v2.go | 7 ++++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/providers/squadcast/deduplocation_rules_v2.go b/providers/squadcast/deduplocation_rules_v2.go index fd38f19c6c..4e832e366a 100644 --- a/providers/squadcast/deduplocation_rules_v2.go +++ b/providers/squadcast/deduplocation_rules_v2.go @@ -2,6 +2,7 @@ package squadcast import ( "fmt" + "strings" "github.com/GoogleCloudPlatform/terraformer/terraformutils" ) @@ -31,8 +32,12 @@ func (g *DeduplicationRuleGenerator) createResources(deduplicationRulesV2 Dedupl func (g *DeduplicationRuleGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { + getServicesURL := "/v3/services" + if strings.TrimSpace(g.Args["team_id"].(string)) != "" { + getServicesURL = fmt.Sprintf("/v3/services?owner_id=%s", g.Args["team_id"].(string)) + } req := TRequest{ - URL: "/v3/services", + URL: getServicesURL, AccessToken: g.Args["access_token"].(string), Region: g.Args["region"].(string), IsAuthenticated: true, diff --git a/providers/squadcast/routing_rules_v2.go b/providers/squadcast/routing_rules_v2.go index eb9964c6d5..74989af272 100644 --- a/providers/squadcast/routing_rules_v2.go +++ b/providers/squadcast/routing_rules_v2.go @@ -2,6 +2,7 @@ package squadcast import ( "fmt" + "strings" "github.com/GoogleCloudPlatform/terraformer/terraformutils" ) @@ -31,8 +32,12 @@ func (g *RoutingRuleGenerator) createResources(routingRules RoutingRules) []terr func (g *RoutingRuleGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { + getServicesURL := "/v3/services" + if strings.TrimSpace(g.Args["team_id"].(string)) != "" { + getServicesURL = fmt.Sprintf("/v3/services?owner_id=%s", g.Args["team_id"].(string)) + } req := TRequest{ - URL: "/v3/services", + URL: getServicesURL, AccessToken: g.Args["access_token"].(string), Region: g.Args["region"].(string), IsAuthenticated: true, diff --git a/providers/squadcast/service.go b/providers/squadcast/service.go index 4bb31276dc..de5cbdfb48 100644 --- a/providers/squadcast/service.go +++ b/providers/squadcast/service.go @@ -4,6 +4,7 @@ package squadcast import ( "fmt" + "strings" "github.com/GoogleCloudPlatform/terraformer/terraformutils" ) @@ -36,8 +37,12 @@ func (g *ServiceGenerator) createResources(services []Service) []terraformutils. } func (g *ServiceGenerator) InitResources() error { + getServicesURL := "/v3/services" + if strings.TrimSpace(g.Args["team_id"].(string)) != "" { + getServicesURL = fmt.Sprintf("/v3/services?owner_id=%s", g.Args["team_id"].(string)) + } req := TRequest{ - URL: "/v3/services", + URL: getServicesURL, AccessToken: g.Args["access_token"].(string), Region: g.Args["region"].(string), IsAuthenticated: true, diff --git a/providers/squadcast/suppression_rule_v2.go b/providers/squadcast/suppression_rule_v2.go index 279850077a..59519150d8 100644 --- a/providers/squadcast/suppression_rule_v2.go +++ b/providers/squadcast/suppression_rule_v2.go @@ -2,6 +2,7 @@ package squadcast import ( "fmt" + "strings" "github.com/GoogleCloudPlatform/terraformer/terraformutils" ) @@ -31,8 +32,12 @@ func (g *SuppressionRuleGenerator) createResources(suppressionRules SuppressionR func (g *SuppressionRuleGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { + getServicesURL := "/v3/services" + if strings.TrimSpace(g.Args["team_id"].(string)) != "" { + getServicesURL = fmt.Sprintf("/v3/services?owner_id=%s", g.Args["team_id"].(string)) + } req := TRequest{ - URL: "/v3/services", + URL: getServicesURL, AccessToken: g.Args["access_token"].(string), Region: g.Args["region"].(string), IsAuthenticated: true, diff --git a/providers/squadcast/tagging_rule_v2.go b/providers/squadcast/tagging_rule_v2.go index aee7a72dad..c653927b72 100644 --- a/providers/squadcast/tagging_rule_v2.go +++ b/providers/squadcast/tagging_rule_v2.go @@ -2,6 +2,7 @@ package squadcast import ( "fmt" + "strings" "github.com/GoogleCloudPlatform/terraformer/terraformutils" ) @@ -31,8 +32,12 @@ func (g *TaggingRuleGenerator) createResources(taggingRule TaggingRules) []terra func (g *TaggingRuleGenerator) InitResources() error { if len(g.Args["service_name"].(string)) == 0 { + getServicesURL := "/v3/services" + if strings.TrimSpace(g.Args["team_id"].(string)) != "" { + getServicesURL = fmt.Sprintf("/v3/services?owner_id=%s", g.Args["team_id"].(string)) + } req := TRequest{ - URL: "/v3/services", + URL: getServicesURL, AccessToken: g.Args["access_token"].(string), Region: g.Args["region"].(string), IsAuthenticated: true,