Skip to content

Commit

Permalink
[Refactoring] Devspace Controller (#2182)
Browse files Browse the repository at this point in the history
* ♻️ (DevSpace) Follow up Tom's suggestions on PR#2086

Add more validations and nil check in devspace.

* Fix the format issue of devspace controller.

* Fix the TestBase64String issue.

* Simplify the error message for flattening sku.

Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com>

* Update documentation for name of sku.

Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com>

* Update the documentation for tier

Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com>

* add `'s`

Co-Authored-By: metacpp <1684739+metacpp@users.noreply.github.com>

* Simplify the code to set controller property fields.

* Fixing the formatting
  • Loading branch information
metacpp authored and tombuildsstuff committed Oct 31, 2018
1 parent a4241b0 commit 206ff8a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
30 changes: 30 additions & 0 deletions azurerm/helpers/validate/base64.go
Original file line number Diff line number Diff line change
@@ -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
}
}
35 changes: 35 additions & 0 deletions azurerm/helpers/validate/base64_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
27 changes: 12 additions & 15 deletions azurerm/resource_arm_devspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func resourceArmDevSpaceController() *schema.Resource {
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.Base64String(),
},

"tags": tagsSchema(),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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}
Expand Down
20 changes: 10 additions & 10 deletions website/docs/r/devspace_controller.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand Down

0 comments on commit 206ff8a

Please sign in to comment.