diff --git a/azurerm/helpers/validate/base64.go b/azurerm/helpers/validate/base64.go new file mode 100644 index 000000000000..116a563073f8 --- /dev/null +++ b/azurerm/helpers/validate/base64.go @@ -0,0 +1,30 @@ +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..5acfbb32f54c --- /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: 1, + }, + { + 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 7b88b9231b4e..c1cf445002ab 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,20 +171,14 @@ 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`: %+v", 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) - } + d.Set("host_suffix", props.HostSuffix) + d.Set("data_plane_fqdn", props.DataPlaneFqdn) + d.Set("target_container_host_resource_id", props.TargetContainerHostResourceID) } flattenAndSetTags(d, result.Tags) @@ -264,7 +258,10 @@ func flattenDevSpaceControllerSku(skuObj *devspaces.Sku) []interface{} { } skuConfig := make(map[string]interface{}) - 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..20218f3267ec 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 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 the only 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'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