-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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_service_fabric_mesh_local_network
#8838
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} | ||
|
||
cluster := ServiceFabricMeshNetworkId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if cluster.Name, err = id.PopSegment("networks"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cluster, 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
175 changes: 175 additions & 0 deletions
175
...erm/internal/services/servicefabricmesh/resource_arm_service_fabric_mesh_local_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,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth checking for empty string here too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call |
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy-paste error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. Updated!