From 0d0cb41d7c0e4d2722063c784f394d26c32f53ff Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Thu, 9 Feb 2023 13:56:59 +0900 Subject: [PATCH 1/6] add edge deployment provider resources --- docs/resources/edge_deployment.md | 32 +++++++++++ docs/resources/edge_deployment_service.md | 34 ++++++++++++ .../sigsci_edge_deployment/resource.tf | 3 + .../resource.tf | 4 ++ go.mod | 2 +- go.sum | 4 +- provider/config.go | 32 ++++++++--- provider/provider.go | 18 ++++-- provider/resource_edge_deployment.go | 42 ++++++++++++++ provider/resource_edge_deployment_service.go | 55 +++++++++++++++++++ 10 files changed, 212 insertions(+), 14 deletions(-) create mode 100644 docs/resources/edge_deployment.md create mode 100644 docs/resources/edge_deployment_service.md create mode 100644 examples/resources/sigsci_edge_deployment/resource.tf create mode 100644 examples/resources/sigsci_edge_deployment_service/resource.tf create mode 100644 provider/resource_edge_deployment.go create mode 100644 provider/resource_edge_deployment_service.go diff --git a/docs/resources/edge_deployment.md b/docs/resources/edge_deployment.md new file mode 100644 index 0000000..8d50b1f --- /dev/null +++ b/docs/resources/edge_deployment.md @@ -0,0 +1,32 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sigsci_edge_deployment Resource - terraform-provider-sigsci" +subcategory: "" +description: |- + +--- + +# sigsci_edge_deployment (Resource) + + + +## Example Usage + +```terraform +resource "sigsci_edge_deployment" { + site_short_name = "manual_test" +} +``` + + +## Schema + +### Required + +- `site_short_name` (String) Site short name + +### Read-Only + +- `id` (String) The ID of this resource. + + diff --git a/docs/resources/edge_deployment_service.md b/docs/resources/edge_deployment_service.md new file mode 100644 index 0000000..7a8fadf --- /dev/null +++ b/docs/resources/edge_deployment_service.md @@ -0,0 +1,34 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "sigsci_edge_deployment_service Resource - terraform-provider-sigsci" +subcategory: "" +description: |- + +--- + +# sigsci_edge_deployment_service (Resource) + + + +## Example Usage + +```terraform +resource "sigsci_site" "my-site" { + site_short_name = "manual_test" + fastly_sid = "test_sid" +} +``` + + +## Schema + +### Required + +- `fastly_sid` (String) Fastly service ID +- `site_short_name` (String) Site short name + +### Read-Only + +- `id` (String) The ID of this resource. + + diff --git a/examples/resources/sigsci_edge_deployment/resource.tf b/examples/resources/sigsci_edge_deployment/resource.tf new file mode 100644 index 0000000..75b78c4 --- /dev/null +++ b/examples/resources/sigsci_edge_deployment/resource.tf @@ -0,0 +1,3 @@ +resource "sigsci_edge_deployment" { + site_short_name = "manual_test" +} diff --git a/examples/resources/sigsci_edge_deployment_service/resource.tf b/examples/resources/sigsci_edge_deployment_service/resource.tf new file mode 100644 index 0000000..40556cf --- /dev/null +++ b/examples/resources/sigsci_edge_deployment_service/resource.tf @@ -0,0 +1,4 @@ +resource "sigsci_site" "my-site" { + site_short_name = "manual_test" + fastly_sid = "test_sid" +} diff --git a/go.mod b/go.mod index 84f1c25..dca84d8 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/terraform-plugin-docs v0.13.0 github.com/hashicorp/terraform-plugin-sdk v1.14.0 - github.com/signalsciences/go-sigsci v0.1.8 + github.com/signalsciences/go-sigsci v0.1.9 golang.org/x/lint v0.0.0-20190409202823-959b441ac422 honnef.co/go/tools v0.3.3 ) diff --git a/go.sum b/go.sum index dba6048..e2f71e3 100644 --- a/go.sum +++ b/go.sum @@ -263,8 +263,8 @@ github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/signalsciences/go-sigsci v0.1.8 h1:vTWe3beT9lsaWnvoPsItPspk/9tKriDjeAJTruC4oo4= -github.com/signalsciences/go-sigsci v0.1.8/go.mod h1:9eUL/FIXlslxnqtsf0yk67CSBiYdL4ToYjYybzWQ77A= +github.com/signalsciences/go-sigsci v0.1.9 h1:A8ipxyBWCkamFU0CAuhH6C0oZpveO6HrlIXE+ejO9mU= +github.com/signalsciences/go-sigsci v0.1.9/go.mod h1:MjtGKaRj55suCf+CVUm4QN26wVn/2b1Syux/xU8/jOs= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= diff --git a/provider/config.go b/provider/config.go index 02251fc..e51ae41 100644 --- a/provider/config.go +++ b/provider/config.go @@ -8,10 +8,11 @@ import ( //Config struct for email and password type Config struct { - URL string - Email string - Password string - APIToken string + URL string + Email string + Password string + APIToken string + FastlyKey string } //Client returns a signal science client @@ -24,11 +25,28 @@ func (c *Config) Client() (interface{}, error) { return nil, fmt.Errorf("email cannot be empty") } + if c.APIToken == "" && c.Password == "" { + return nil, fmt.Errorf("you must provide either password or api_token") + } + + var ( + client sigsci.Client + err error + ) + if c.APIToken != "" { - return sigsci.NewTokenClient(c.Email, c.APIToken), nil + client = sigsci.NewTokenClient(c.Email, c.APIToken) } else if c.Password != "" { - return sigsci.NewClient(c.Email, c.Password) + client, err = sigsci.NewClient(c.Email, c.Password) + + if err != nil { + return nil, err + } + } + + if c.FastlyKey != "" { + client.SetFastlyKey(c.FastlyKey) } - return nil, fmt.Errorf("you must provide either password or api_token") + return client, nil } diff --git a/provider/provider.go b/provider/provider.go index a93940f..3b5f881 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -38,6 +38,13 @@ func Provider() terraform.ResourceProvider { Sensitive: true, AtLeastOneOf: []string{"password", "auth_token"}, }, + "fastly_key": { + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("FASTLY_KEY", nil), + Description: "The Fastly API key used for deploying Signal Sciences as a Fastly edge security service. For edge deployment service calls, the Fastly key must have write access to the given service.", + Sensitive: true, + }, "api_url": { Type: schema.TypeString, Optional: true, @@ -64,6 +71,8 @@ func Provider() terraform.ResourceProvider { "sigsci_corp_integration": resourceCorpIntegration(), "sigsci_corp_cloudwaf_instance": resourceCorpCloudWAFInstance(), "sigsci_corp_cloudwaf_certificate": resourceCorpCloudWAFCertificate(), + "sigsci_edge_deployment": resourceEdgeDeployment(), + "sigsci_edge_deployment_service": resourceEdgeDeploymentService(), }, } provider.ConfigureFunc = providerConfigure() @@ -73,10 +82,11 @@ func Provider() terraform.ResourceProvider { func providerConfigure() schema.ConfigureFunc { return func(d *schema.ResourceData) (interface{}, error) { config := Config{ - Email: d.Get("email").(string), - Password: d.Get("password").(string), - APIToken: d.Get("auth_token").(string), - URL: d.Get("api_url").(string), + Email: d.Get("email").(string), + Password: d.Get("password").(string), + APIToken: d.Get("auth_token").(string), + FastlyKey: d.Get("fastly_key").(string), + URL: d.Get("api_url").(string), } client, err := config.Client() if err != nil { diff --git a/provider/resource_edge_deployment.go b/provider/resource_edge_deployment.go new file mode 100644 index 0000000..b0603f2 --- /dev/null +++ b/provider/resource_edge_deployment.go @@ -0,0 +1,42 @@ +package provider + +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceEdgeDeployment() *schema.Resource { + return &schema.Resource{ + Create: createOrUpdateEdgeDeployment, + Read: readEdgeDeployment, + Update: createOrUpdateEdgeDeployment, + Delete: deleteEdgeDeployment, + Importer: &schema.ResourceImporter{}, + Schema: map[string]*schema.Schema{ + "site_short_name": { + Type: schema.TypeString, + Description: "Site short name", + Required: true, + }, + }, + } +} + +func createOrUpdateEdgeDeployment(d *schema.ResourceData, m interface{}) error { + pm := m.(providerMetadata) + sc := pm.Client + + d.SetId(d.Get("site_short_name").(string)) + + return sc.CreateOrUpdateEdgeDeployment(pm.Corp, d.Get("site_short_name").(string)) +} + +func readEdgeDeployment(d *schema.ResourceData, m interface{}) error { + return nil +} + +func deleteEdgeDeployment(d *schema.ResourceData, m interface{}) error { + pm := m.(providerMetadata) + sc := pm.Client + + return sc.DeleteEdgeDeployment(pm.Corp, d.Get("site_short_name").(string)) +} diff --git a/provider/resource_edge_deployment_service.go b/provider/resource_edge_deployment_service.go new file mode 100644 index 0000000..66f02c2 --- /dev/null +++ b/provider/resource_edge_deployment_service.go @@ -0,0 +1,55 @@ +package provider + +import ( + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func resourceEdgeDeploymentService() *schema.Resource { + return &schema.Resource{ + Create: createOrUpdateEdgeDeploymentService, + Read: readEdgeDeploymentService, + Update: updateEdgeDeploymentBackends, + Delete: detachEdgeDeploymentService, + Importer: &schema.ResourceImporter{}, + Schema: map[string]*schema.Schema{ + "site_short_name": { + Type: schema.TypeString, + Description: "Site short name", + Required: true, + }, + + "fastly_sid": { + Type: schema.TypeString, + Description: "Fastly service ID", + Required: true, + }, + }, + } +} + +func createOrUpdateEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { + pm := m.(providerMetadata) + sc := pm.Client + + d.SetId(d.Get("site_short_name").(string)) + + return sc.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) +} + +func readEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { + return nil +} + +func updateEdgeDeploymentBackends(d *schema.ResourceData, m interface{}) error { + pm := m.(providerMetadata) + sc := pm.Client + + return sc.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) +} + +func detachEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { + pm := m.(providerMetadata) + sc := pm.Client + + return sc.DetachEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) +} From 9d9f58c7035051a17420dc908dd25fbf1c93ce59 Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Mon, 13 Feb 2023 14:15:49 +0900 Subject: [PATCH 2/6] Update docs/resources/edge_deployment_service.md Co-authored-by: Jon Nangle --- docs/resources/edge_deployment_service.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/edge_deployment_service.md b/docs/resources/edge_deployment_service.md index 7a8fadf..63ba3ef 100644 --- a/docs/resources/edge_deployment_service.md +++ b/docs/resources/edge_deployment_service.md @@ -13,7 +13,7 @@ description: |- ## Example Usage ```terraform -resource "sigsci_site" "my-site" { +resource "sigsci_edge_deployment_service" "my-service" { site_short_name = "manual_test" fastly_sid = "test_sid" } From 201b22b3bc4abc511084fa6eb8aee7b3ab684acc Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Mon, 13 Feb 2023 14:16:14 +0900 Subject: [PATCH 3/6] Update examples/resources/sigsci_edge_deployment_service/resource.tf Co-authored-by: Jon Nangle --- examples/resources/sigsci_edge_deployment_service/resource.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/resources/sigsci_edge_deployment_service/resource.tf b/examples/resources/sigsci_edge_deployment_service/resource.tf index 40556cf..a2e3fcf 100644 --- a/examples/resources/sigsci_edge_deployment_service/resource.tf +++ b/examples/resources/sigsci_edge_deployment_service/resource.tf @@ -1,4 +1,4 @@ -resource "sigsci_site" "my-site" { +resource "sigsci_edge_deployment_service" "my-service" { site_short_name = "manual_test" fastly_sid = "test_sid" } From b2cf45b2c560e88fbf74ca8ab165118085bf79ba Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Tue, 14 Feb 2023 08:40:36 +0900 Subject: [PATCH 4/6] use go-sigsci v0.1.10 to allow 200 responses for DELETE calls --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index dca84d8..94c80e3 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/hashicorp/terraform-plugin-docs v0.13.0 github.com/hashicorp/terraform-plugin-sdk v1.14.0 - github.com/signalsciences/go-sigsci v0.1.9 + github.com/signalsciences/go-sigsci v0.1.10 golang.org/x/lint v0.0.0-20190409202823-959b441ac422 honnef.co/go/tools v0.3.3 ) diff --git a/go.sum b/go.sum index e2f71e3..6e2c3f7 100644 --- a/go.sum +++ b/go.sum @@ -263,8 +263,8 @@ github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/signalsciences/go-sigsci v0.1.9 h1:A8ipxyBWCkamFU0CAuhH6C0oZpveO6HrlIXE+ejO9mU= -github.com/signalsciences/go-sigsci v0.1.9/go.mod h1:MjtGKaRj55suCf+CVUm4QN26wVn/2b1Syux/xU8/jOs= +github.com/signalsciences/go-sigsci v0.1.10 h1:3sv/ADf3p54X5gevHL54I9XDYVKzWgQiHabrJvzVKOU= +github.com/signalsciences/go-sigsci v0.1.10/go.mod h1:MjtGKaRj55suCf+CVUm4QN26wVn/2b1Syux/xU8/jOs= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= From ffea4a51d0108224d85db2326cdf8c9968771e12 Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Tue, 14 Feb 2023 08:46:17 +0900 Subject: [PATCH 5/6] use fastly_sid as edge_deployment_service ID --- provider/resource_edge_deployment_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/resource_edge_deployment_service.go b/provider/resource_edge_deployment_service.go index 66f02c2..7e94ae9 100644 --- a/provider/resource_edge_deployment_service.go +++ b/provider/resource_edge_deployment_service.go @@ -31,7 +31,7 @@ func createOrUpdateEdgeDeploymentService(d *schema.ResourceData, m interface{}) pm := m.(providerMetadata) sc := pm.Client - d.SetId(d.Get("site_short_name").(string)) + d.SetId(d.Get("fastly_sid").(string)) return sc.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) } From e2a33ddaa65eab5a0ab12ebc1eb1aa35e8aea030 Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Thu, 16 Feb 2023 08:33:11 +0900 Subject: [PATCH 6/6] fix whitespace, use pm.Client directly rather than copying client first --- provider/config.go | 1 - provider/resource_edge_deployment.go | 6 ++---- provider/resource_edge_deployment_service.go | 9 +++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/provider/config.go b/provider/config.go index e51ae41..b78dcb5 100644 --- a/provider/config.go +++ b/provider/config.go @@ -38,7 +38,6 @@ func (c *Config) Client() (interface{}, error) { client = sigsci.NewTokenClient(c.Email, c.APIToken) } else if c.Password != "" { client, err = sigsci.NewClient(c.Email, c.Password) - if err != nil { return nil, err } diff --git a/provider/resource_edge_deployment.go b/provider/resource_edge_deployment.go index b0603f2..891204d 100644 --- a/provider/resource_edge_deployment.go +++ b/provider/resource_edge_deployment.go @@ -23,11 +23,10 @@ func resourceEdgeDeployment() *schema.Resource { func createOrUpdateEdgeDeployment(d *schema.ResourceData, m interface{}) error { pm := m.(providerMetadata) - sc := pm.Client d.SetId(d.Get("site_short_name").(string)) - return sc.CreateOrUpdateEdgeDeployment(pm.Corp, d.Get("site_short_name").(string)) + return pm.Client.CreateOrUpdateEdgeDeployment(pm.Corp, d.Get("site_short_name").(string)) } func readEdgeDeployment(d *schema.ResourceData, m interface{}) error { @@ -36,7 +35,6 @@ func readEdgeDeployment(d *schema.ResourceData, m interface{}) error { func deleteEdgeDeployment(d *schema.ResourceData, m interface{}) error { pm := m.(providerMetadata) - sc := pm.Client - return sc.DeleteEdgeDeployment(pm.Corp, d.Get("site_short_name").(string)) + return pm.Client.DeleteEdgeDeployment(pm.Corp, d.Get("site_short_name").(string)) } diff --git a/provider/resource_edge_deployment_service.go b/provider/resource_edge_deployment_service.go index 7e94ae9..80c262c 100644 --- a/provider/resource_edge_deployment_service.go +++ b/provider/resource_edge_deployment_service.go @@ -29,11 +29,10 @@ func resourceEdgeDeploymentService() *schema.Resource { func createOrUpdateEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { pm := m.(providerMetadata) - sc := pm.Client d.SetId(d.Get("fastly_sid").(string)) - return sc.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) + return pm.Client.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) } func readEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { @@ -42,14 +41,12 @@ func readEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { func updateEdgeDeploymentBackends(d *schema.ResourceData, m interface{}) error { pm := m.(providerMetadata) - sc := pm.Client - return sc.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) + return pm.Client.CreateOrUpdateEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) } func detachEdgeDeploymentService(d *schema.ResourceData, m interface{}) error { pm := m.(providerMetadata) - sc := pm.Client - return sc.DetachEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) + return pm.Client.DetachEdgeDeploymentService(pm.Corp, d.Get("site_short_name").(string), d.Get("fastly_sid").(string)) }