-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
azurerm_service_fabric_mesh_local_network
(#8838)
- Loading branch information
Showing
10 changed files
with
528 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
azurerm/internal/services/servicefabricmesh/parse/service_fabric_mesh_network.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
73 changes: 73 additions & 0 deletions
73
azurerm/internal/services/servicefabricmesh/parse/service_fabric_mesh_network_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
175 changes: 175 additions & 0 deletions
175
azurerm/internal/services/servicefabricmesh/service_fabric_mesh_local_network_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
File renamed without changes.
Oops, something went wrong.