Skip to content

Commit

Permalink
New Resource: azurerm_relay_namespace
Browse files Browse the repository at this point in the history
```
$ acctests azurerm TestAccAzureRMRelayNamespace_
=== RUN   TestAccAzureRMRelayNamespace_basic
--- PASS: TestAccAzureRMRelayNamespace_basic (154.19s)
=== RUN   TestAccAzureRMRelayNamespace_complete
--- PASS: TestAccAzureRMRelayNamespace_complete (148.58s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	302.825s
```

Import tests:

```
$ acctests azurerm TestAccAzureRMRelayNamespace_import
=== RUN   TestAccAzureRMRelayNamespace_importBasic
--- PASS: TestAccAzureRMRelayNamespace_importBasic (194.74s)
=== RUN   TestAccAzureRMRelayNamespace_importComplete
--- PASS: TestAccAzureRMRelayNamespace_importComplete (164.46s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	359.235s
```
  • Loading branch information
tombuildsstuff committed May 14, 2018
1 parent 14c0687 commit b08d903
Show file tree
Hide file tree
Showing 7 changed files with 567 additions and 0 deletions.
11 changes: 11 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-04-30-preview/postgresql"
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2016-06-01/recoveryservices"
"github.com/Azure/azure-sdk-for-go/services/redis/mgmt/2018-03-01/redis"
"github.com/Azure/azure-sdk-for-go/services/relay/mgmt/2017-04-01/relay"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-09-01/locks"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-12-01/policy"
Expand Down Expand Up @@ -170,6 +171,9 @@ type ArmClient struct {
// Recovery Services
recoveryServicesVaultsClient recoveryservices.VaultsClient

// Relay
relayNamespacesClient relay.NamespacesClient

// Resources
managementLocksClient locks.ManagementLocksClient
deploymentsClient resources.DeploymentsClient
Expand Down Expand Up @@ -380,6 +384,7 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
client.registerOperationalInsightsClients(endpoint, c.SubscriptionID, auth, sender)
client.registerRecoveryServiceClients(endpoint, c.SubscriptionID, auth)
client.registerRedisClients(endpoint, c.SubscriptionID, auth, sender)
client.registerRelayClients(endpoint, c.SubscriptionID, auth, sender)
client.registerResourcesClients(endpoint, c.SubscriptionID, auth)
client.registerSearchClients(endpoint, c.SubscriptionID, auth)
client.registerServiceBusClients(endpoint, c.SubscriptionID, auth)
Expand Down Expand Up @@ -812,6 +817,12 @@ func (c *ArmClient) registerRedisClients(endpoint, subscriptionId string, auth a
c.redisPatchSchedulesClient = patchSchedulesClient
}

func (c *ArmClient) registerRelayClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
relayNamespacesClient := relay.NewNamespacesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&relayNamespacesClient.Client, auth)
c.relayNamespacesClient = relayNamespacesClient
}

func (c *ArmClient) registerResourcesClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
locksClient := locks.NewManagementLocksClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&locksClient.Client, auth)
Expand Down
54 changes: 54 additions & 0 deletions azurerm/import_arm_relay_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMRelayNamespace_importBasic(t *testing.T) {
resourceName := "azurerm_relay_namespace.test"

ri := acctest.RandInt()
config := testAccAzureRMRelayNamespace_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRelayNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMRelayNamespace_importComplete(t *testing.T) {
resourceName := "azurerm_relay_namespace.test"

ri := acctest.RandInt()
config := testAccAzureRMRelayNamespace_complete(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRelayNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_postgresql_firewall_rule": resourceArmPostgreSQLFirewallRule(),
"azurerm_postgresql_server": resourceArmPostgreSQLServer(),
"azurerm_public_ip": resourceArmPublicIp(),
"azurerm_relay_namespace": resourceArmRelayNamespace(),
"azurerm_recovery_services_vault": resourceArmRecoveryServicesVault(),
"azurerm_redis_cache": resourceArmRedisCache(),
"azurerm_redis_firewall_rule": resourceArmRedisFirewallRule(),
Expand Down
257 changes: 257 additions & 0 deletions azurerm/resource_arm_relay_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package azurerm

import (
"context"
"fmt"
"log"

"time"

"github.com/Azure/azure-sdk-for-go/services/relay/mgmt/2017-04-01/relay"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmRelayNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceArmRelayNamespaceCreateUpdate,
Read: resourceArmRelayNamespaceRead,
Update: resourceArmRelayNamespaceCreateUpdate,
Delete: resourceArmRelayNamespaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(6, 50),
},

"location": locationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"sku": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(relay.Standard),
}, true),
},
},
},
},

"metric_id": {
Type: schema.TypeString,
Computed: true,
},

"primary_connection_string": {
Type: schema.TypeString,
Computed: true,
},

"secondary_connection_string": {
Type: schema.TypeString,
Computed: true,
},

"primary_key": {
Type: schema.TypeString,
Computed: true,
},

"secondary_key": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}

func resourceArmRelayNamespaceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).relayNamespacesClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for Relay Namespace creation.")

name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)

sku := expandRelayNamespaceSku(d)
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)

parameters := relay.Namespace{
Location: utils.String(location),
Sku: sku,
NamespaceProperties: &relay.NamespaceProperties{},
Tags: expandedTags,
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
if err != nil {
return err
}

err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return err
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Relay Namespace %q (resource group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmRelayNamespaceRead(d, meta)
}

func resourceArmRelayNamespaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).relayNamespacesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["namespaces"]

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Relay Namespace %q (Resource Group %q): %s", name, resourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if sku := resp.Sku; sku != nil {
flattenedSku := flattenRelayNamespaceSku(sku)
if err := d.Set("sku", flattenedSku); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
}
}

if props := resp.NamespaceProperties; props != nil {
d.Set("metric_id", props.MetricID)
}

keysResp, err := client.ListKeys(ctx, resourceGroup, name, "RootManageSharedAccessKey")
if err != nil {
return fmt.Errorf("Error making ListKeys request on Relay Namespace %q (Resource Group %q): %s", name, resourceGroup, err)
}

d.Set("primary_connection_string", keysResp.PrimaryConnectionString)
d.Set("primary_key", keysResp.PrimaryKey)
d.Set("secondary_connection_string", keysResp.SecondaryConnectionString)
d.Set("secondary_key", keysResp.SecondaryKey)

flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmRelayNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).relayNamespacesClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["namespaces"]

future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}

return err
}

// we can't make use of the Future here due to a bug where 404 isn't tracked as Successful
log.Printf("[DEBUG] Waiting for Relay Namespace %q (Resource Group %q) to be deleted", name, resourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"Pending"},
Target: []string{"Deleted"},
Refresh: relayNamespaceDeleteRefreshFunc(ctx, client, resourceGroup, name),
Timeout: 60 * time.Minute,
MinTimeout: 15 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Relay Namespace %q (Resource Group %q) to be deleted: %s", name, resourceGroup, err)
}

return nil
}

func relayNamespaceDeleteRefreshFunc(ctx context.Context, client relay.NamespacesClient, resourceGroupName string, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroupName, name)
if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return res, "Deleted", nil
}

return nil, "Error", fmt.Errorf("Error issuing read request in relayNamespaceDeleteRefreshFunc to Relay Namespace %q (Resource Group %q): %s", name, resourceGroupName, err)
}

return res, "Pending", nil
}
}

func expandRelayNamespaceSku(d *schema.ResourceData) *relay.Sku {
vs := d.Get("sku").([]interface{})
v := vs[0].(map[string]interface{})

name := v["name"].(string)

return &relay.Sku{
Name: utils.String(name),
Tier: relay.SkuTier(name),
}
}

func flattenRelayNamespaceSku(input *relay.Sku) []interface{} {
if input == nil {
return []interface{}{}
}

output := make(map[string]interface{}, 0)
if name := input.Name; name != nil {
output["name"] = *name
}
return []interface{}{output}
}
Loading

0 comments on commit b08d903

Please sign in to comment.