Skip to content

Commit

Permalink
New Resource: azurerm_service_fabric_mesh_local_network (#8838)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Oct 16, 2020
1 parent f49f49e commit 84120b6
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 1 deletion.
5 changes: 5 additions & 0 deletions azurerm/internal/services/servicefabricmesh/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ import (

type Client struct {
ApplicationClient *servicefabricmesh.ApplicationClient
NetworkClient *servicefabricmesh.NetworkClient
ServiceClient *servicefabricmesh.ServiceClient
}

func NewClient(o *common.ClientOptions) *Client {
applicationsClient := servicefabricmesh.NewApplicationClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&applicationsClient.Client, o.ResourceManagerAuthorizer)

networksClient := servicefabricmesh.NewNetworkClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&networksClient.Client, o.ResourceManagerAuthorizer)

servicesClient := servicefabricmesh.NewServiceClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&servicesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ApplicationClient: &applicationsClient,
NetworkClient: &networksClient,
ServiceClient: &servicesClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ServiceFabricMeshNetworkId struct {
ResourceGroup string
Name string
}

func ServiceFabricMeshNetworkID(input string) (*ServiceFabricMeshNetworkId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Service Fabric Mesh Network ID %q: %+v", input, err)
}

network := ServiceFabricMeshNetworkId{
ResourceGroup: id.ResourceGroup,
}

if network.Name, err = id.PopSegment("networks"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &network, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package parse

import (
"testing"
)

func TestServiceFabricMeshNetworkId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *ServiceFabricMeshNetworkId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Networks Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceFabricMesh/networks/",
Expected: nil,
},
{
Name: "Service Fabric Mesh Network ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceFabricMesh/networks/Network1",
Expected: &ServiceFabricMeshNetworkId{
Name: "Network1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceFabricMesh/Networks/Network1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ServiceFabricMeshNetworkID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
3 changes: 2 additions & 1 deletion azurerm/internal/services/servicefabricmesh/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_service_fabric_mesh_application": resourceArmServiceFabricMeshApplication(),
"azurerm_service_fabric_mesh_application": resourceArmServiceFabricMeshApplication(),
"azurerm_service_fabric_mesh_local_network": resourceArmServiceFabricMeshLocalNetwork(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package servicefabricmesh

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/servicefabricmesh/mgmt/2018-09-01-preview/servicefabricmesh"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicefabricmesh/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmServiceFabricMeshLocalNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceArmServiceFabricMeshLocalNetworkCreateUpdate,
Read: resourceArmServiceFabricMeshLocalNetworkRead,
Update: resourceArmServiceFabricMeshLocalNetworkCreateUpdate,
Delete: resourceArmServiceFabricMeshLocalNetworkDelete,
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ServiceFabricMeshNetworkID(id)
return err
}),

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: validation.StringIsNotEmpty,
},

// Follow casing issue here https://github.com/Azure/azure-rest-api-specs/issues/9330
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"location": azure.SchemaLocation(),

"network_address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"tags": tags.Schema(),
},
}
}

func resourceArmServiceFabricMeshLocalNetworkCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ServiceFabricMesh.NetworkClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
location := location.Normalize(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Service Fabric Mesh Local Network: %+v", err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_service_fabric_mesh_local_network", *existing.ID)
}
}

parameters := servicefabricmesh.NetworkResourceDescription{
Properties: &servicefabricmesh.LocalNetworkResourceProperties{
Description: utils.String(d.Get("description").(string)),
Kind: servicefabricmesh.KindLocal,
NetworkAddressPrefix: utils.String(d.Get("network_address_prefix").(string)),
},
Location: utils.String(location),
Tags: tags.Expand(t),
}

if _, err := client.Create(ctx, resourceGroup, name, parameters); err != nil {
return fmt.Errorf("creating Service Fabric Mesh Local Network %q (Resource Group %q): %+v", name, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Service Fabric Mesh Local Network %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("client returned a nil ID for Service Fabric Mesh Local Network %q", name)
}

d.SetId(*resp.ID)

return resourceArmServiceFabricMeshLocalNetworkRead(d, meta)
}

func resourceArmServiceFabricMeshLocalNetworkRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ServiceFabricMesh.NetworkClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ServiceFabricMeshNetworkID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Unable to find Service Fabric Mesh Local Network %q - removing from state", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("reading Service Fabric Mesh Local Network: %+v", err)
}

props, ok := resp.Properties.AsLocalNetworkResourceProperties()
if !ok {
return fmt.Errorf("classifiying Service Fabric Mesh Local Network %q (Resource Group %q): Expected: %q Received: %q", id.Name, id.ResourceGroup, servicefabricmesh.KindNetworkResourceProperties, props.Kind)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("network_address_prefix", props.NetworkAddressPrefix)
d.Set("description", props.Description)

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmServiceFabricMeshLocalNetworkDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ServiceFabricMesh.NetworkClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ServiceFabricMeshNetworkID(d.Id())
if err != nil {
return err
}

resp, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !response.WasNotFound(resp.Response) {
return fmt.Errorf("deleting Service Fabric Mesh Local Network %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}

return nil
}
Loading

0 comments on commit 84120b6

Please sign in to comment.