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_hybrid_connection #4832

Merged
merged 20 commits into from
Nov 8, 2019
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
9 changes: 7 additions & 2 deletions azurerm/internal/services/relay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (
)

type Client struct {
NamespacesClient *relay.NamespacesClient
NamespacesClient *relay.NamespacesClient
HybridConnectionsClient *relay.HybridConnectionsClient
}

func BuildClient(o *common.ClientOptions) *Client {
NamespacesClient := relay.NewNamespacesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&NamespacesClient.Client, o.ResourceManagerAuthorizer)

HybridConnectionsClient := relay.NewHybridConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&HybridConnectionsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
NamespacesClient: &NamespacesClient,
NamespacesClient: &NamespacesClient,
HybridConnectionsClient: &HybridConnectionsClient,
}
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_recovery_services_vault": resourceArmRecoveryServicesVault(),
"azurerm_redis_cache": resourceArmRedisCache(),
"azurerm_redis_firewall_rule": resourceArmRedisFirewallRule(),
"azurerm_relay_hybrid_connection": resourceArmHybridConnection(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved up one to keep this list alphabetical

"azurerm_relay_namespace": resourceArmRelayNamespace(),
"azurerm_resource_group": resourceArmResourceGroup(),
"azurerm_role_assignment": resourceArmRoleAssignment(),
Expand Down
193 changes: 193 additions & 0 deletions azurerm/resource_arm_relay_hybrid_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
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-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmHybridConnection() *schema.Resource {
return &schema.Resource{
Create: resourceArmHybridConnectionCreateUpdate,
Read: resourceArmHybridConnectionRead,
Update: resourceArmHybridConnectionCreateUpdate,
Delete: resourceArmHybridConnectionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"relay_namespace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"requires_client_authorization": {
Type: schema.TypeBool,
Default: true,
ForceNew: true,
Optional: true,
},
"user_metadata": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
}
}

func resourceArmHybridConnectionCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Relay.HybridConnectionsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Relay Hybrid Connection creation.")

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
relayNamespace := d.Get("relay_namespace_name").(string)
requireClientAuthroization := d.Get("requires_client_authorization").(bool)
userMetadata := d.Get("user_metadata").(string)

parameters := relay.HybridConnection{
HybridConnectionProperties: &relay.HybridConnectionProperties{
RequiresClientAuthorization: &requireClientAuthroization,
UserMetadata: &userMetadata,
},
}

_, err := client.CreateOrUpdate(ctx, resourceGroup, relayNamespace, name, parameters)
if err != nil {
return fmt.Errorf("Error creating Relay Hybrid Connection %q (Namespace %q Resource Group %q): %+v", name, relayNamespace, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, relayNamespace, name)
if err != nil {
return fmt.Errorf("Error issuing get request for Relay Hybrid Connection %q (Namespace %q Resource Group %q): %+v", name, relayNamespace, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Relay Hybrid Connection %q (Namespace %q Resource group %s) ID", name, relayNamespace, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmHybridConnectionRead(d, meta)
}

func resourceArmHybridConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Relay.HybridConnectionsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()

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

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

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

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("relay_namespace_name", relayNamespace)

if props := resp.HybridConnectionProperties; props != nil {
d.Set("requires_client_authorization", props.RequiresClientAuthorization)
d.Set("user_metadata", props.UserMetadata)
}

return nil
}

func resourceArmHybridConnectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Relay.HybridConnectionsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()

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

log.Printf("[INFO] Waiting for Relay Hybrid Connection %q (Namespace %q Resource Group %q) to be deleted", name, relayNamespace, resourceGroup)
rc, err := client.Delete(ctx, resourceGroup, relayNamespace, name)

if err != nil {
if response.WasNotFound(rc.Response) {
return nil
}

return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{"Pending"},
Target: []string{"Deleted"},
Refresh: hybridConnectionDeleteRefreshFunc(ctx, client, resourceGroup, relayNamespace, name),
Timeout: 30 * time.Minute,
MinTimeout: 15 * time.Second,
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Relay Hybrid Connection %q (Namespace %q Resource Group %q) to be deleted: %s", name, relayNamespace, resourceGroup, err)
}

return nil
}

func hybridConnectionDeleteRefreshFunc(ctx context.Context, client *relay.HybridConnectionsClient, resourceGroupName string, relayNamespace string, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroupName, relayNamespace, 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 Hybrid Connection %q (Namespace %q Resource Group %q): %s", name, relayNamespace, resourceGroupName, err)
}

return res, "Pending", nil
}
}
Loading