Skip to content

Commit

Permalink
New Data Source/Resource: azurerm_cdn_frontdoor_rule_set (#17094)
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff authored Jun 9, 2022
1 parent 0e71d61 commit 2d248af
Show file tree
Hide file tree
Showing 20 changed files with 1,745 additions and 0 deletions.
69 changes: 69 additions & 0 deletions internal/services/cdn/cdn_frontdoor_rule_set_data_source.go
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 internal/services/cdn/cdn_frontdoor_rule_set_data_source_test.go
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 internal/services/cdn/cdn_frontdoor_rule_set_resource.go
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 internal/services/cdn/cdn_frontdoor_rule_set_resource_test.go
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)
}
Loading

0 comments on commit 2d248af

Please sign in to comment.