forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add azurerm_route_filter for hashicorp#1083
- Loading branch information
Christian Pearce
committed
Apr 1, 2020
1 parent
7d17d6a
commit 3c59d32
Showing
11 changed files
with
770 additions
and
0 deletions.
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
65 changes: 65 additions & 0 deletions
65
azurerm/internal/services/network/data_source_route_filter.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,65 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"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/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceArmRouteFilter() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmRouteFilterRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"location": azure.SchemaLocationForDataSource(), | ||
|
||
"tags": tags.SchemaDataSource(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmRouteFilterRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.RouteFiltersClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error: Route Filter %q (Resource Group %q) was not found", name, resourceGroup) | ||
} | ||
return fmt.Errorf("Error making Read request on Azure Route Filter %q: %+v", name, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resourceGroup) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} |
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
161 changes: 161 additions & 0 deletions
161
azurerm/internal/services/network/resource_arm_route_filter.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,161 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network" | ||
"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/features" | ||
"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 resourceArmRouteFilter() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmRouteFilterCreateUpdate, | ||
Read: resourceArmRouteFilterRead, | ||
Update: resourceArmRouteFilterCreateUpdate, | ||
Delete: resourceArmRouteFilterDelete, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { | ||
_, err := ParseRouteFilterID(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, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"tags": tags.Schema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmRouteFilterCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.RouteFiltersClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
log.Printf("[INFO] preparing arguments for Route Filter create/update.") | ||
|
||
name := d.Get("name").(string) | ||
location := azure.NormalizeLocation(d.Get("location").(string)) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
t := d.Get("tags").(map[string]interface{}) | ||
|
||
if features.ShouldResourcesBeImported() && d.IsNewResource() { | ||
existing, err := client.Get(ctx, resourceGroup, name, "") | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Error checking for presence of existing Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_route_filter", *existing.ID) | ||
} | ||
} | ||
|
||
routeSet := network.RouteFilter{ | ||
Name: &name, | ||
Location: &location, | ||
Tags: tags.Expand(t), | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, routeSet) | ||
if err != nil { | ||
return fmt.Errorf("Error Creating/Updating Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for completion of Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
read, err := client.Get(ctx, resourceGroup, name, "") | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("ID was nil for Route Filter %q (Resource Group %q)", name, resourceGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmRouteFilterRead(d, meta) | ||
} | ||
|
||
func resourceArmRouteFilterRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.RouteFiltersClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := ParseRouteFilterID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "") | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error retrieving Route Table %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
d.Set("name", id.Name) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} | ||
|
||
func resourceArmRouteFilterDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.RouteFiltersClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := ParseRouteFilterID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
future, err := client.Delete(ctx, id.ResourceGroup, id.Name) | ||
if err != nil { | ||
if !response.WasNotFound(future.Response()) { | ||
return fmt.Errorf("Error deleting Route Filter %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for deletion of Route Filter %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
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,36 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
// NOTE: there's some nice things we can do with this around validation | ||
// since these top level objects exist | ||
|
||
type RouteFilterResourceID struct { | ||
ResourceGroup string | ||
Name string | ||
} | ||
|
||
func ParseRouteFilterID(input string) (*RouteFilterResourceID, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERROR] Unable to parse Route Filter ID %q: %+v", input, err) | ||
} | ||
|
||
routeFilter := RouteFilterResourceID{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if routeFilter.Name, err = id.PopSegment("routeFilters"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &routeFilter, nil | ||
} |
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,58 @@ | ||
package network | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseRouteFilter(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *RouteFilterResourceID | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Route Filter Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Route Filter Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/routeFilters/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Completed", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/routeFilters/example", | ||
Expected: &RouteFilterResourceID{ | ||
Name: "example", | ||
ResourceGroup: "foo", | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := ParseRouteFilterID(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 ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
Oops, something went wrong.