-
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 Data Source/Resource: azurerm_cdn_frontdoor_rule_set
#17094
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9ebab2
CDN: generating the Resource ID Parsers/Validators for Rule Sets/Rules
tombuildsstuff 2cc3a8c
New Resource: `azurerm_cdn_frontdoor_rule_set`
tombuildsstuff 9703e7a
New Data Source: `azurerm_cdn_frontdoor_rule_set`
tombuildsstuff 82c1683
Update cdn_frontdoor_rule_set_resource_test.go
mbfrahry 3fd2033
Update cdn_frontdoor_rule_set_resource_test.go
mbfrahry 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
69 changes: 69 additions & 0 deletions
69
internal/services/cdn/cdn_frontdoor_rule_set_data_source.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,69 @@ | ||
package cdn | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/validate" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
func dataSourceCdnFrontDoorRuleSet() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Read: dataSourceCdnFrontDoorRuleSetRead, | ||
|
||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
Read: pluginsdk.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.FrontDoorRuleSetName, | ||
}, | ||
|
||
"profile_name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.FrontDoorName, | ||
}, | ||
|
||
"resource_group_name": commonschema.ResourceGroupNameForDataSource(), | ||
|
||
"cdn_frontdoor_profile_id": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceCdnFrontDoorRuleSetRead(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Cdn.FrontDoorRuleSetsClient | ||
subscriptionId := meta.(*clients.Client).Account.SubscriptionId | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id := parse.NewFrontDoorRuleSetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("profile_name").(string), d.Get("name").(string)) | ||
resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("%s was not found", id) | ||
} | ||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
d.SetId(id.ID()) | ||
d.Set("name", id.RuleSetName) | ||
d.Set("profile_name", id.ProfileName) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
d.Set("cdn_frontdoor_profile_id", parse.NewFrontDoorProfileID(id.SubscriptionId, id.ResourceGroup, id.ProfileName).ID()) | ||
|
||
return nil | ||
} |
37 changes: 37 additions & 0 deletions
37
internal/services/cdn/cdn_frontdoor_rule_set_data_source_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,37 @@ | ||
package cdn_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
) | ||
|
||
type CdnFrontDoorRuleSetDataSource struct{} | ||
|
||
func TestAccCdnFrontDoorRuleSetDataSource_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_cdn_frontdoor_rule_set", "test") | ||
d := CdnFrontDoorRuleSetDataSource{} | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: d.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("cdn_frontdoor_profile_id").MatchesOtherKey(check.That("azurerm_cdn_frontdoor_profile.test").Key("id")), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (CdnFrontDoorRuleSetDataSource) basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
%s | ||
|
||
data "azurerm_cdn_frontdoor_rule_set" "test" { | ||
name = azurerm_cdn_frontdoor_rule_set.test.name | ||
profile_name = azurerm_cdn_frontdoor_rule_set.test.profile_name | ||
resource_group_name = azurerm_cdn_frontdoor_rule_set.test.resource_group_name | ||
} | ||
`, CdnFrontDoorRuleSetResource{}.complete(data)) | ||
} |
129 changes: 129 additions & 0 deletions
129
internal/services/cdn/cdn_frontdoor_rule_set_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,129 @@ | ||
package cdn | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/validate" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
func resourceCdnFrontDoorRuleSet() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Create: resourceCdnFrontDoorRuleSetCreate, | ||
Read: resourceCdnFrontDoorRuleSetRead, | ||
Delete: resourceCdnFrontDoorRuleSetDelete, | ||
|
||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
Create: pluginsdk.DefaultTimeout(30 * time.Minute), | ||
Read: pluginsdk.DefaultTimeout(5 * time.Minute), | ||
Update: pluginsdk.DefaultTimeout(30 * time.Minute), | ||
Delete: pluginsdk.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { | ||
_, err := parse.FrontDoorRuleSetID(id) | ||
return err | ||
}), | ||
|
||
Schema: map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.FrontDoorRuleSetName, | ||
}, | ||
|
||
"cdn_frontdoor_profile_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.FrontDoorProfileID, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCdnFrontDoorRuleSetCreate(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Cdn.FrontDoorRuleSetsClient | ||
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
profileId, err := parse.FrontDoorProfileID(d.Get("cdn_frontdoor_profile_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id := parse.NewFrontDoorRuleSetID(profileId.SubscriptionId, profileId.ResourceGroup, profileId.ProfileName, d.Get("name").(string)) | ||
if d.IsNewResource() { | ||
existing, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for existing %s: %+v", id, err) | ||
} | ||
} | ||
|
||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return tf.ImportAsExistsError("azurerm_cdn_frontdoor_rule_set", id.ID()) | ||
} | ||
} | ||
|
||
if _, err = client.Create(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
d.SetId(id.ID()) | ||
return resourceCdnFrontDoorRuleSetRead(d, meta) | ||
} | ||
|
||
func resourceCdnFrontDoorRuleSetRead(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Cdn.FrontDoorRuleSetsClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.FrontDoorRuleSetID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
d.Set("name", id.RuleSetName) | ||
d.Set("cdn_frontdoor_profile_id", parse.NewFrontDoorProfileID(id.SubscriptionId, id.ResourceGroup, id.ProfileName).ID()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCdnFrontDoorRuleSetDelete(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Cdn.FrontDoorRuleSetsClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.FrontDoorRuleSetID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
future, err := client.Delete(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName) | ||
if err != nil { | ||
return fmt.Errorf("deleting %s: %+v", *id, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("waiting for the deletion of %s: %+v", *id, err) | ||
} | ||
|
||
return nil | ||
} |
135 changes: 135 additions & 0 deletions
135
internal/services/cdn/cdn_frontdoor_rule_set_resource_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,135 @@ | ||
package cdn_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cdn/parse" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
type CdnFrontDoorRuleSetResource struct{} | ||
|
||
func TestAccCdnFrontDoorRuleSet_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_rule_set", "test") | ||
r := CdnFrontDoorRuleSetResource{} | ||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccCdnFrontDoorRuleSet_requiresImport(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_rule_set", "test") | ||
r := CdnFrontDoorRuleSetResource{} | ||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.RequiresImportErrorStep(r.requiresImport), | ||
}) | ||
} | ||
|
||
func TestAccCdnFrontDoorRuleSet_complete(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_rule_set", "test") | ||
r := CdnFrontDoorRuleSetResource{} | ||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.complete(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func (r CdnFrontDoorRuleSetResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { | ||
id, err := parse.FrontDoorRuleSetID(state.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := clients.Cdn.FrontDoorRuleSetsClient | ||
resp, err := client.Get(ctx, id.ResourceGroup, id.ProfileName, id.RuleSetName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return utils.Bool(false), nil | ||
} | ||
return nil, fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
return utils.Bool(true), nil | ||
} | ||
|
||
func (r CdnFrontDoorRuleSetResource) basic(data acceptance.TestData) string { | ||
template := r.template(data) | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
%s | ||
resource "azurerm_cdn_frontdoor_rule_set" "test" { | ||
name = "acctestfdruleset%d" | ||
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.test.id | ||
} | ||
`, template, data.RandomIntOfLength(8)) | ||
} | ||
|
||
func (r CdnFrontDoorRuleSetResource) requiresImport(data acceptance.TestData) string { | ||
config := r.basic(data) | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_cdn_frontdoor_rule_set" "import" { | ||
name = azurerm_cdn_frontdoor_rule_set.test.name | ||
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.test.id | ||
} | ||
`, config) | ||
} | ||
|
||
func (r CdnFrontDoorRuleSetResource) complete(data acceptance.TestData) string { | ||
template := r.template(data) | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
%s | ||
resource "azurerm_cdn_frontdoor_rule_set" "test" { | ||
name = "acctestfdruleset%d" | ||
cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.test.id | ||
} | ||
`, template, data.RandomIntOfLength(8)) | ||
} | ||
|
||
func (CdnFrontDoorRuleSetResource) template(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestrg-cdn-afdx-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_cdn_frontdoor_profile" "test" { | ||
name = "acctest-fdprofile-%d" | ||
resource_group_name = azurerm_resource_group.test.name | ||
sku_name = "Standard_AzureFrontDoor" | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} |
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.
Should this be the same format as the resource with just
name
andcdn_frontdoor_profile_id
?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.
nope, that's intentional since users may be referencing the Rule Set without the parents details