Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_relay_namespace #1233

Merged
merged 3 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
},
})
}
2 changes: 2 additions & 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 Expand Up @@ -342,6 +343,7 @@ func determineAzureResourceProvidersToRegister(providerList []resources.Provider
"microsoft.insights": {},
"Microsoft.Network": {},
"Microsoft.OperationalInsights": {},
"Microsoft.Relay": {},
"Microsoft.Resources": {},
"Microsoft.Search": {},
"Microsoft.ServiceBus": {},
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