From bdaa8351c85d774856c238899c9697e2e67a5fad Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Mon, 29 Oct 2018 15:31:13 -0700 Subject: [PATCH 1/9] :recycle: (DevSpace) Follow up Tom's suggestions on PR#2086 Add more validations and nil check in devspace. --- azurerm/helpers/validate/base64.go | 29 +++++++++++++++ azurerm/helpers/validate/base64_test.go | 35 +++++++++++++++++++ azurerm/resource_arm_devspace_controller.go | 31 ++++++++-------- .../docs/r/devspace_controller.html.markdown | 20 +++++------ 4 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 azurerm/helpers/validate/base64.go create mode 100644 azurerm/helpers/validate/base64_test.go diff --git a/azurerm/helpers/validate/base64.go b/azurerm/helpers/validate/base64.go new file mode 100644 index 000000000000..f76ac9c40a9a --- /dev/null +++ b/azurerm/helpers/validate/base64.go @@ -0,0 +1,29 @@ +package validate + +import ( + "encoding/base64" + "fmt" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" +) + +func Base64String() schema.SchemaValidateFunc { + return func(i interface{}, k string) (s []string, es []error) { + // Empty string is not allowed + if s, es = validation.NoZeroValues(i, k); len(es) > 0 { + return + } + + v, ok := i.(string) + if !ok { + es = append(es, fmt.Errorf("expected type of %s to be string", k)) + return + } + + if _, err := base64.StdEncoding.DecodeString(v); err != nil { + es = append(es, fmt.Errorf("expect value (%s) of %s is base64 string", v, k)) + } + + return + } +} diff --git a/azurerm/helpers/validate/base64_test.go b/azurerm/helpers/validate/base64_test.go new file mode 100644 index 000000000000..37d4859e1418 --- /dev/null +++ b/azurerm/helpers/validate/base64_test.go @@ -0,0 +1,35 @@ +package validate + +import ( + "testing" +) + +func TestBase64String(t *testing.T) { + cases := []struct { + Input string + Errors int + }{ + { + Input: "", + Errors: 0, + }, + { + Input: "aGVsbG8td29ybGQ=", + Errors: 0, + }, + { + Input: "hello-world", + Errors: 1, + }, + } + + for _, tc := range cases { + t.Run(tc.Input, func(t *testing.T) { + _, errors := Base64String()(tc.Input, "base64") + + if len(errors) != tc.Errors { + t.Fatalf("Expected Base64 string to have %d not %d errors for %q: %v", tc.Errors, len(errors), tc.Input, errors) + } + }) + } +} diff --git a/azurerm/resource_arm_devspace_controller.go b/azurerm/resource_arm_devspace_controller.go index 03da69161158..a7a4f82e9715 100644 --- a/azurerm/resource_arm_devspace_controller.go +++ b/azurerm/resource_arm_devspace_controller.go @@ -80,7 +80,7 @@ func resourceArmDevSpaceController() *schema.Resource { Required: true, ForceNew: true, Sensitive: true, - ValidateFunc: validation.NoZeroValues, + ValidateFunc: validate.Base64String(), }, "tags": tagsSchema(), @@ -162,7 +162,7 @@ func resourceArmDevSpaceControllerRead(d *schema.ResourceData, meta interface{}) return nil } - return fmt.Errorf("Error making Read request on DevSpace Controller Lab %q (Resource Group %q): %+v", name, resGroupName, err) + return fmt.Errorf("Error making Read request on DevSpace Controller %q (Resource Group %q): %+v", name, resGroupName, err) } d.Set("name", result.Name) @@ -171,21 +171,20 @@ func resourceArmDevSpaceControllerRead(d *schema.ResourceData, meta interface{}) d.Set("location", azureRMNormalizeLocation(*location)) } - d.Set("sku", flattenDevSpaceControllerSku(result.Sku)) + if err := d.Set("sku", flattenDevSpaceControllerSku(result.Sku)); err != nil { + return fmt.Errorf("Error flattenning Sku for DevSpace Controller %q (Resource Group %q): %+v", name, resGroupName, err) + } - if props := result.ControllerProperties; props != nil { - if props.HostSuffix != nil { - d.Set("host_suffix", props.HostSuffix) - } - if props.DataPlaneFqdn != nil { - d.Set("data_plane_fqdn", props.DataPlaneFqdn) - } - - if props.TargetContainerHostResourceID != nil { - d.Set("target_container_host_resource_id", props.TargetContainerHostResourceID) - } + var hostSuffix, dataPlaneFqdn, targetContainerHostResourceID *string + if props := result.ControllerProperties; props != nil { + hostSuffix = props.HostSuffix + dataPlaneFqdn = props.DataPlaneFqdn + targetContainerHostResourceID = props.TargetContainerHostResourceID } + d.Set("host_suffix", hostSuffix) + d.Set("data_plane_fqdn", dataPlaneFqdn) + d.Set("target_container_host_resource_id", targetContainerHostResourceID) flattenAndSetTags(d, result.Tags) @@ -264,7 +263,9 @@ func flattenDevSpaceControllerSku(skuObj *devspaces.Sku) []interface{} { } skuConfig := make(map[string]interface{}, 0) - skuConfig["name"] = *skuObj.Name + if skuObj.Name != nil { + skuConfig["name"] = *skuObj.Name + } skuConfig["tier"] = skuObj.Tier return []interface{}{skuConfig} diff --git a/website/docs/r/devspace_controller.html.markdown b/website/docs/r/devspace_controller.html.markdown index 7b22e8000dc1..f1235fdab28c 100644 --- a/website/docs/r/devspace_controller.html.markdown +++ b/website/docs/r/devspace_controller.html.markdown @@ -3,12 +3,12 @@ layout: "azurerm" page_title: "Azure Resource Manager: azurerm_devspace_controller" sidebar_current: "docs-azurerm-resource-devspace-controller" description: |- - Manages an Azure DevSpace Controller. + Manages a DevSpace Controller. --- # azurerm_devspace_controller -Manages an Azure DevSpace Controller. +Manages a DevSpace Controller. ## Example Usage @@ -60,15 +60,15 @@ resource "azurerm_devspace_controller" test { The following arguments are supported: -* `name` - (Required) Specifies the name of the Azure DevSpace Controller. Changing this forces a new resource to be created. +* `name` - (Required) Specifies the name of the DevSpace Controller. Changing this forces a new resource to be created. -* `resource_group_name` - (Required) The name of the resource group under which the Azure DevSpace Controller resource has to be created. Changing this forces a new resource to be created. +* `resource_group_name` - (Required) The name of the resource group under which the DevSpace Controller resource has to be created. Changing this forces a new resource to be created. -* `location` - (Required) Specifies the supported Azure location where the Azure DevSpace Controller should exist. Changing this forces a new resource to be created. +* `location` - (Required) Specifies the supported location where the DevSpace Controller should exist. Changing this forces a new resource to be created. * `sku` - (Required) A `sku` block as documented below. Changing this forces a new resource to be created. -* `host_suffix` - (Required) The host suffix for the Azure DevSpace Controller. Changing this forces a new resource to be created. +* `host_suffix` - (Required) The host suffix for the DevSpace Controller. Changing this forces a new resource to be created. * `target_container_host_resource_id` - (Required) The resource id of Azure Kubernetes Service cluster. Changing this forces a new resource to be created. @@ -80,20 +80,20 @@ The following arguments are supported: A `sku` block supports the following: -* `name` - (Required) The name of the SKU for Azure DevSpace Controller. Currently supported value is `S1`. Changing this forces a new resource to be created. -* `tier` - (Required) The tier of the SKU for Azure DevSpace Controller. Currently supported value is `Standard`. Changing this forces a new resource to be created. +* `name` - (Required) The name of the SKU for DevSpace Controller. Currently supported value is `S1`. Changing this forces a new resource to be created. +* `tier` - (Required) The tier of the SKU for DevSpace Controller. Currently supported value is `Standard`. Changing this forces a new resource to be created. ## Attributes Reference The following attributes are exported: -* `id` - The ID of the Azure DevSpace Controller. +* `id` - The ID of the DevSpace Controller. * `data_plane_fqdn` - DNS name for accessing DataPlane services. ## Import -Azure DevSpace Controller can be imported using the `resource id`, e.g. +DevSpace Controller can be imported using the `resource id`, e.g. ```shell terraform import azurerm_devspace_controller.controller1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.DevSpaces/controllers/controller1Name From acbb73bce37cf4c0ac5e6b7bcdfce18120c6e73e Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Mon, 29 Oct 2018 16:37:22 -0700 Subject: [PATCH 2/9] Fix the format issue of devspace controller. --- azurerm/resource_arm_devspace_controller.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/resource_arm_devspace_controller.go b/azurerm/resource_arm_devspace_controller.go index a7a4f82e9715..cbf28becf9bd 100644 --- a/azurerm/resource_arm_devspace_controller.go +++ b/azurerm/resource_arm_devspace_controller.go @@ -175,7 +175,6 @@ func resourceArmDevSpaceControllerRead(d *schema.ResourceData, meta interface{}) return fmt.Errorf("Error flattenning Sku for DevSpace Controller %q (Resource Group %q): %+v", name, resGroupName, err) } - var hostSuffix, dataPlaneFqdn, targetContainerHostResourceID *string if props := result.ControllerProperties; props != nil { hostSuffix = props.HostSuffix From 57d1b726bdb3395da9028a89d541f960651c71ae Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Mon, 29 Oct 2018 16:50:19 -0700 Subject: [PATCH 3/9] Fix the TestBase64String issue. --- azurerm/helpers/validate/base64_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/helpers/validate/base64_test.go b/azurerm/helpers/validate/base64_test.go index 37d4859e1418..5acfbb32f54c 100644 --- a/azurerm/helpers/validate/base64_test.go +++ b/azurerm/helpers/validate/base64_test.go @@ -11,7 +11,7 @@ func TestBase64String(t *testing.T) { }{ { Input: "", - Errors: 0, + Errors: 1, }, { Input: "aGVsbG8td29ybGQ=", From 66b612850ce76e989593521154ac9ce546897057 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Tue, 30 Oct 2018 16:10:25 -0700 Subject: [PATCH 4/9] Simplify the error message for flattening sku. Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com> --- azurerm/resource_arm_devspace_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_devspace_controller.go b/azurerm/resource_arm_devspace_controller.go index cbf28becf9bd..d5b3be40ec20 100644 --- a/azurerm/resource_arm_devspace_controller.go +++ b/azurerm/resource_arm_devspace_controller.go @@ -172,7 +172,7 @@ func resourceArmDevSpaceControllerRead(d *schema.ResourceData, meta interface{}) } if err := d.Set("sku", flattenDevSpaceControllerSku(result.Sku)); err != nil { - return fmt.Errorf("Error flattenning Sku for DevSpace Controller %q (Resource Group %q): %+v", name, resGroupName, err) + return fmt.Errorf("Error flattening`sku`: %+v", err) } var hostSuffix, dataPlaneFqdn, targetContainerHostResourceID *string From 48ed6c2b0b4b3ccc25042edee5041f5aa27193ac Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Tue, 30 Oct 2018 16:15:25 -0700 Subject: [PATCH 5/9] Update documentation for name of sku. Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com> --- website/docs/r/devspace_controller.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/devspace_controller.html.markdown b/website/docs/r/devspace_controller.html.markdown index f1235fdab28c..449481f449ad 100644 --- a/website/docs/r/devspace_controller.html.markdown +++ b/website/docs/r/devspace_controller.html.markdown @@ -80,7 +80,7 @@ The following arguments are supported: A `sku` block supports the following: -* `name` - (Required) The name of the SKU for DevSpace Controller. Currently supported value is `S1`. Changing this forces a new resource to be created. +* `name` - (Required) The name of the SKU for DevSpace Controller. Currently the only supported value is `S1`. Changing this forces a new resource to be created. * `tier` - (Required) The tier of the SKU for DevSpace Controller. Currently supported value is `Standard`. Changing this forces a new resource to be created. ## Attributes Reference From c09a91a86208672f713db3c43f346e29018a03a9 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Tue, 30 Oct 2018 16:16:13 -0700 Subject: [PATCH 6/9] Update the documentation for tier Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com> --- website/docs/r/devspace_controller.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/devspace_controller.html.markdown b/website/docs/r/devspace_controller.html.markdown index 449481f449ad..2b56d6f09887 100644 --- a/website/docs/r/devspace_controller.html.markdown +++ b/website/docs/r/devspace_controller.html.markdown @@ -81,7 +81,7 @@ The following arguments are supported: A `sku` block supports the following: * `name` - (Required) The name of the SKU for DevSpace Controller. Currently the only supported value is `S1`. Changing this forces a new resource to be created. -* `tier` - (Required) The tier of the SKU for DevSpace Controller. Currently supported value is `Standard`. Changing this forces a new resource to be created. +* `tier` - (Required) The tier of the SKU for DevSpace Controller. Currently the only supported value is `Standard`. Changing this forces a new resource to be created. ## Attributes Reference From dca2d4fc2933f9a6984350a7fcda5440852ed7d7 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Tue, 30 Oct 2018 16:18:02 -0700 Subject: [PATCH 7/9] add `'s` Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com> --- website/docs/r/devspace_controller.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/devspace_controller.html.markdown b/website/docs/r/devspace_controller.html.markdown index 2b56d6f09887..20218f3267ec 100644 --- a/website/docs/r/devspace_controller.html.markdown +++ b/website/docs/r/devspace_controller.html.markdown @@ -93,7 +93,7 @@ The following attributes are exported: ## Import -DevSpace Controller can be imported using the `resource id`, e.g. +DevSpace Controller's can be imported using the `resource id`, e.g. ```shell terraform import azurerm_devspace_controller.controller1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.DevSpaces/controllers/controller1Name From 0b6f109d22b06c603ff0d0a47ffcfcc7df788c9a Mon Sep 17 00:00:00 2001 From: Su Shi <1684739+metacpp@users.noreply.github.com> Date: Tue, 30 Oct 2018 16:20:24 -0700 Subject: [PATCH 8/9] Simplify the code to set controller property fields. --- azurerm/resource_arm_devspace_controller.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/azurerm/resource_arm_devspace_controller.go b/azurerm/resource_arm_devspace_controller.go index cbf28becf9bd..dc6448fe0c07 100644 --- a/azurerm/resource_arm_devspace_controller.go +++ b/azurerm/resource_arm_devspace_controller.go @@ -172,18 +172,14 @@ func resourceArmDevSpaceControllerRead(d *schema.ResourceData, meta interface{}) } if err := d.Set("sku", flattenDevSpaceControllerSku(result.Sku)); err != nil { - return fmt.Errorf("Error flattenning Sku for DevSpace Controller %q (Resource Group %q): %+v", name, resGroupName, err) + return fmt.Errorf("Error flattenning `sku`: %+v", err) } - var hostSuffix, dataPlaneFqdn, targetContainerHostResourceID *string if props := result.ControllerProperties; props != nil { - hostSuffix = props.HostSuffix - dataPlaneFqdn = props.DataPlaneFqdn - targetContainerHostResourceID = props.TargetContainerHostResourceID + d.Set("host_suffix", props.HostSuffix) + d.Set("data_plane_fqdn", props.DataPlaneFqdn) + d.Set("target_container_host_resource_id", props.TargetContainerHostResourceID) } - d.Set("host_suffix", hostSuffix) - d.Set("data_plane_fqdn", dataPlaneFqdn) - d.Set("target_container_host_resource_id", targetContainerHostResourceID) flattenAndSetTags(d, result.Tags) From 4cd95d1f0730c50fbca545dec532ac76b81fe39c Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 31 Oct 2018 13:06:07 +0000 Subject: [PATCH 9/9] Fixing the formatting --- azurerm/helpers/validate/base64.go | 1 + azurerm/resource_arm_devspace_controller.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/azurerm/helpers/validate/base64.go b/azurerm/helpers/validate/base64.go index f76ac9c40a9a..116a563073f8 100644 --- a/azurerm/helpers/validate/base64.go +++ b/azurerm/helpers/validate/base64.go @@ -3,6 +3,7 @@ package validate import ( "encoding/base64" "fmt" + "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" ) diff --git a/azurerm/resource_arm_devspace_controller.go b/azurerm/resource_arm_devspace_controller.go index e47990d14ce0..c1cf445002ab 100644 --- a/azurerm/resource_arm_devspace_controller.go +++ b/azurerm/resource_arm_devspace_controller.go @@ -261,7 +261,7 @@ func flattenDevSpaceControllerSku(skuObj *devspaces.Sku) []interface{} { if skuObj.Name != nil { skuConfig["name"] = *skuObj.Name } - + skuConfig["tier"] = skuObj.Tier return []interface{}{skuConfig}