Skip to content

Commit

Permalink
Initial Check-in...
Browse files Browse the repository at this point in the history
  • Loading branch information
WodansSon committed Dec 13, 2024
1 parent 8649171 commit df13089
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/services/cdn/cdn_frontdoor_profile_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/profiles"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand All @@ -35,6 +36,8 @@ func dataSourceCdnFrontDoorProfile() *pluginsdk.Resource {

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),

"response_timeout_seconds": {
Type: pluginsdk.TypeInt,
Computed: true,
Expand Down Expand Up @@ -81,6 +84,10 @@ func dataSourceCdnFrontDoorProfileRead(d *pluginsdk.ResourceData, meta interface
d.Set("sku_name", string(pointer.From(skuName)))
}

if identity, err := identity.FlattenSystemAndUserAssignedMap(model.Identity); err == nil {
d.Set("identity", identity)
}

if props := model.Properties; props != nil {
d.Set("response_timeout_seconds", int(pointer.From(props.OriginResponseTimeoutSeconds)))

Expand Down
75 changes: 75 additions & 0 deletions internal/services/cdn/cdn_frontdoor_profile_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,51 @@ func TestAccCdnFrontDoorProfileDataSource_basic(t *testing.T) {
})
}

func TestAccCdnFrontDoorProfileDataSource_basicWithSystemIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_cdn_frontdoor_profile", "test")
d := CdnFrontDoorProfileDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.basicWithSystemIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_AzureFrontDoor"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned"),
),
},
})
}

func TestAccCdnFrontDoorProfileDataSource_basicWithUserIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_cdn_frontdoor_profile", "test")
d := CdnFrontDoorProfileDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.basicWithUserIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_AzureFrontDoor"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("UserAssigned"),
),
},
})
}

func TestAccCdnFrontDoorProfileDataSource_basicWithSystemAndUserIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_cdn_frontdoor_profile", "test")
d := CdnFrontDoorProfileDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: d.basicWithSystemAndUserIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_AzureFrontDoor"),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned, UserAssigned"),
),
},
})
}

func (CdnFrontDoorProfileDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand All @@ -37,3 +82,33 @@ data "azurerm_cdn_frontdoor_profile" "test" {
}
`, CdnFrontDoorProfileResource{}.complete(data))
}

func (CdnFrontDoorProfileDataSource) basicWithSystemIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_cdn_frontdoor_profile" "test" {
name = azurerm_cdn_frontdoor_profile.test.name
resource_group_name = azurerm_cdn_frontdoor_profile.test.resource_group_name
}
`, CdnFrontDoorProfileResource{}.basicWithSystemIdentity(data))
}

func (CdnFrontDoorProfileDataSource) basicWithUserIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_cdn_frontdoor_profile" "test" {
name = azurerm_cdn_frontdoor_profile.test.name
resource_group_name = azurerm_cdn_frontdoor_profile.test.resource_group_name
}
`, CdnFrontDoorProfileResource{}.basicWithUserIdentity(data))
}

func (CdnFrontDoorProfileDataSource) basicWithSystemAndUserIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
data "azurerm_cdn_frontdoor_profile" "test" {
name = azurerm_cdn_frontdoor_profile.test.name
resource_group_name = azurerm_cdn_frontdoor_profile.test.resource_group_name
}
`, CdnFrontDoorProfileResource{}.basicWithSystemAndUserIdentity(data))
}
30 changes: 30 additions & 0 deletions internal/services/cdn/cdn_frontdoor_profile_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/cdn/2024-02-01/profiles"
Expand Down Expand Up @@ -50,6 +51,8 @@ func resourceCdnFrontDoorProfile() *pluginsdk.Resource {

"resource_group_name": commonschema.ResourceGroupName(),

"identity": commonschema.SystemAssignedUserAssignedIdentityOptional(),

"response_timeout_seconds": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -107,6 +110,15 @@ func resourceCdnFrontDoorProfileCreate(d *pluginsdk.ResourceData, meta interface
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if v, ok := d.GetOk("identity"); ok {
i, err := identity.ExpandSystemAndUserAssignedMap(v.([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

props.Identity = i
}

err = client.CreateThenPoll(ctx, id, props)
if err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
Expand Down Expand Up @@ -143,6 +155,15 @@ func resourceCdnFrontDoorProfileRead(d *pluginsdk.ResourceData, meta interface{}
d.Set("sku_name", string(pointer.From(skuName)))
}

identity, err := identity.FlattenSystemAndUserAssignedMap(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}

if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if props := model.Properties; props != nil {
d.Set("response_timeout_seconds", int(pointer.From(props.OriginResponseTimeoutSeconds)))

Expand Down Expand Up @@ -178,6 +199,15 @@ func resourceCdnFrontDoorProfileUpdate(d *pluginsdk.ResourceData, meta interface
props.Properties.OriginResponseTimeoutSeconds = pointer.To(int64(d.Get("response_timeout_seconds").(int)))
}

if d.HasChange("identity") {
i, err := identity.ExpandSystemAndUserAssignedMap(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

props.Identity = i
}

err = client.UpdateThenPoll(ctx, pointer.From(id), props)
if err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
Expand Down
Loading

0 comments on commit df13089

Please sign in to comment.