From 445b1fa8dbd70f7c31259c2cb798dbf24833afce Mon Sep 17 00:00:00 2001 From: Giulio Collura Date: Mon, 29 May 2023 10:29:01 +0200 Subject: [PATCH] allow importing on_call_schedule(s) and reviewer_stage(s) (#1) (#1637) --- docs/opal.md | 4 +++ go.mod | 2 +- go.sum | 4 +-- providers/opal/on_call_schedule.go | 46 ++++++++++++++++++++++++++++++ providers/opal/opal_provider.go | 16 +++++++---- 5 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 providers/opal/on_call_schedule.go diff --git a/docs/opal.md b/docs/opal.md index 94fda9417..6a36beed3 100644 --- a/docs/opal.md +++ b/docs/opal.md @@ -68,3 +68,7 @@ $ terraform plan # No changes. Your infrastructure matches the configuration. * `opal_resource` * `group` * `opal_group` +* `on_call_schedules` + * `opal_on_call_schedules` +* `message_channels` + * `opal_message_channels` diff --git a/go.mod b/go.mod index fc2cd74a3..7b75c0fcb 100644 --- a/go.mod +++ b/go.mod @@ -349,6 +349,7 @@ require ( github.com/DataDog/datadog-api-client-go/v2 v2.11.0 github.com/Myra-Security-GmbH/myrasec-go/v2 v2.28.0 github.com/manicminer/hamilton v0.44.0 + github.com/opalsecurity/opal-go v1.0.19 gopkg.in/ns1/ns1-go.v2 v2.6.5 ) @@ -376,7 +377,6 @@ require ( require ( github.com/aws/aws-sdk-go-v2/service/medialive v1.24.2 - github.com/opalsecurity/opal-go v1.0.9 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.0.392 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.392 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb v1.0.392 diff --git a/go.sum b/go.sum index f60ce17a4..e7ce9b256 100644 --- a/go.sum +++ b/go.sum @@ -1188,8 +1188,8 @@ github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je4 github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= -github.com/opalsecurity/opal-go v1.0.9 h1:NlP3K15cpEYwtwHMnpp9TbzaLILUtcTm+OQax0a8p88= -github.com/opalsecurity/opal-go v1.0.9/go.mod h1:bzD4vZIbH+lKhsX8NJ5ISNU2Xgm2qzjj6O9G2ycj58c= +github.com/opalsecurity/opal-go v1.0.19 h1:w9JPghoq9ks3/Br/KC/9h8jiJVlY3JJ/k/imq/pMVd8= +github.com/opalsecurity/opal-go v1.0.19/go.mod h1:G7QQIi36kI3kiTl/Dp8AvLDNoui9jqFOSUthcZ0aof4= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI= diff --git a/providers/opal/on_call_schedule.go b/providers/opal/on_call_schedule.go new file mode 100644 index 000000000..df39d200d --- /dev/null +++ b/providers/opal/on_call_schedule.go @@ -0,0 +1,46 @@ +package opal + +import ( + "context" + "fmt" + + "github.com/GoogleCloudPlatform/terraformer/terraformutils" +) + +type OnCallScheduleGenerator struct { + OpalService +} + +func (g *OnCallScheduleGenerator) InitResources() error { + client, err := g.newClient() + if err != nil { + return fmt.Errorf("unable to list opal on call schedules: %v", err) + } + + onCallSchedules, _, err := client.OnCallSchedulesApi.GetOnCallSchedules(context.TODO()).Execute() + if err != nil { + return fmt.Errorf("unable to list opal on call schedules: %v", err) + } + + countByName := make(map[string]int) + + for _, onCallSchedule := range onCallSchedules.OnCallSchedules { + name := normalizeResourceName(*onCallSchedule.Name) + if count, ok := countByName[name]; ok { + countByName[name] = count + 1 + name = normalizeResourceName(fmt.Sprintf("%s_%d", *onCallSchedule.Name, count+1)) + } else { + countByName[name] = 1 + } + + g.Resources = append(g.Resources, terraformutils.NewSimpleResource( + *onCallSchedule.OnCallScheduleId, + name, + "opal_on_call_schedule", + "opal", + []string{}, + )) + } + + return nil +} diff --git a/providers/opal/opal_provider.go b/providers/opal/opal_provider.go index 5c7393aec..a933d4dfc 100644 --- a/providers/opal/opal_provider.go +++ b/providers/opal/opal_provider.go @@ -53,19 +53,22 @@ func (p OpalProvider) GetResourceConnections() map[string]map[string][]string { "resource": { "owner": { "admin_owner_id", "id", - "reviewer.id", "id", + "reviewer_stage.reviewer.id", "id", }, "group": {"visibility_group.id", "id"}, }, "group": { "owner": { "admin_owner_id", "id", - "reviewer.id", "id", + "reviewer_stage.reviewer.id", "id", }, "group": {"visibility_group.id", "id"}, "message_channel": { "audit_message_channel.id", "id", }, + "on_call_schedule": { + "on_call_schedule.id", "id", + }, }, "owner": { "message_channel": { @@ -113,9 +116,10 @@ func (p *OpalProvider) InitService(serviceName string, verbose bool) error { func (p *OpalProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { return map[string]terraformutils.ServiceGenerator{ - "owner": &OwnerGenerator{}, - "resource": &ResourceGenerator{}, - "group": &GroupGenerator{}, - "message_channel": &MessageChannelGenerator{}, + "owner": &OwnerGenerator{}, + "resource": &ResourceGenerator{}, + "group": &GroupGenerator{}, + "message_channel": &MessageChannelGenerator{}, + "on_call_schedule": &OnCallScheduleGenerator{}, } }