Skip to content
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

azurerm_security_center_subscription_pricing: Upgrade API, add subplan argument #19273

Merged
merged 4 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.1.2
github.com/hashicorp/go-azure-helpers v0.47.0
github.com/hashicorp/go-azure-sdk v0.20221108.1145701
github.com/hashicorp/go-azure-sdk v0.20221114.1064854
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-azure-helpers v0.12.0/go.mod h1:Zc3v4DNeX6PDdy7NljlYpnrdac1++qNW0I4U+ofGwpg=
github.com/hashicorp/go-azure-helpers v0.47.0 h1:E90ZN2hqMtzI+tfGWCnNtyLZYG4csoKSs+hWZZ8ywSM=
github.com/hashicorp/go-azure-helpers v0.47.0/go.mod h1:WiJNl0fD6PoM/MYuGTZ8yuzIaXQR3m2H2g6+EJ8nSwc=
github.com/hashicorp/go-azure-sdk v0.20221108.1145701 h1:unYddTcN7aoC8NAS5o1jPRHrkCEsYeKoVGv4F5RJdlQ=
github.com/hashicorp/go-azure-sdk v0.20221108.1145701/go.mod h1:BEjoURzcFwd+K3MqkbOt9jArIIrsqpBQ2Gz6DdemFIs=
github.com/hashicorp/go-azure-sdk v0.20221114.1064854 h1:2prpRSDjhQtk6ftkBCnCX6NfEXjmN2UQjQHj5vmrsR8=
github.com/hashicorp/go-azure-sdk v0.20221114.1064854/go.mod h1:BEjoURzcFwd+K3MqkbOt9jArIIrsqpBQ2Gz6DdemFIs=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down
5 changes: 3 additions & 2 deletions internal/services/securitycenter/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security"
pricings_v2022_03_01 "github.com/hashicorp/go-azure-sdk/resource-manager/security/2022-03-01/pricings"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

Expand All @@ -11,7 +12,7 @@ type Client struct {
ContactsClient *security.ContactsClient
DeviceSecurityGroupsClient *security.DeviceSecurityGroupsClient
IotSecuritySolutionClient *security.IotSecuritySolutionClient
PricingClient *security.PricingsClient
PricingClient *pricings_v2022_03_01.PricingsClient
WorkspaceClient *security.WorkspaceSettingsClient
AdvancedThreatProtectionClient *security.AdvancedThreatProtectionClient
AutoProvisioningClient *security.AutoProvisioningSettingsClient
Expand All @@ -38,7 +39,7 @@ func NewClient(o *common.ClientOptions) *Client {
IotSecuritySolutionClient := security.NewIotSecuritySolutionClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
o.ConfigureClient(&IotSecuritySolutionClient.Client, o.ResourceManagerAuthorizer)

PricingClient := security.NewPricingsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
PricingClient := pricings_v2022_03_01.NewPricingsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&PricingClient.Client, o.ResourceManagerAuthorizer)

WorkspaceClient := security.NewWorkspaceSettingsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security"
"github.com/hashicorp/go-azure-helpers/lang/response"
pricings_v2022_03_01 "github.com/hashicorp/go-azure-sdk/resource-manager/security/2022-03-01/pricings"
"github.com/hashicorp/terraform-provider-azurerm/utils"

"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/securitycenter/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/securitycenter/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceSecurityCenterSubscriptionPricing() *pluginsdk.Resource {
Expand Down Expand Up @@ -67,6 +70,10 @@ func resourceSecurityCenterSubscriptionPricing() *pluginsdk.Resource {
"Containers",
}, false),
},
"subplan": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
}
}
Expand All @@ -79,14 +86,18 @@ func resourceSecurityCenterSubscriptionPricingUpdate(d *pluginsdk.ResourceData,

// TODO: add a requires import check ensuring this is != Free (meaning we should likely remove Free as a SKU option?)

id := parse.NewPricingID(subscriptionId, d.Get("resource_type").(string))
pricing := security.Pricing{
PricingProperties: &security.PricingProperties{
PricingTier: security.PricingTier(d.Get("tier").(string)),
id := pricings_v2022_03_01.NewPricingID(subscriptionId, d.Get("resource_type").(string))
pricing := pricings_v2022_03_01.Pricing{
Properties: &pricings_v2022_03_01.PricingProperties{
PricingTier: pricings_v2022_03_01.PricingTier(d.Get("tier").(string)),
},
}

if _, err := client.Update(ctx, id.Name, pricing); err != nil {
if v, ok := d.GetOk("subplan"); ok {
pricing.Properties.SubPlan = utils.String(v.(string))
}

if _, err := client.Update(ctx, id, pricing); err != nil {
return fmt.Errorf("setting %s: %+v", id, err)
}

Expand All @@ -99,14 +110,14 @@ func resourceSecurityCenterSubscriptionPricingRead(d *pluginsdk.ResourceData, me
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.PricingID(d.Id())
id, err := pricings_v2022_03_01.ParsePricingID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.Name)
resp, err := client.Get(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[DEBUG] %s was not found - removing from state!", *id)
d.SetId("")
return nil
Expand All @@ -115,9 +126,10 @@ func resourceSecurityCenterSubscriptionPricingRead(d *pluginsdk.ResourceData, me
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("resource_type", id.Name)
if properties := resp.PricingProperties; properties != nil {
d.Set("resource_type", id.PricingName)
if properties := resp.Model.Properties; properties != nil {
favoretti marked this conversation as resolved.
Show resolved Hide resolved
d.Set("tier", properties.PricingTier)
d.Set("subplan", properties.SubPlan)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"testing"

pricings_v2022_03_01 "github.com/hashicorp/go-azure-sdk/resource-manager/security/2022-03-01/pricings"

"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/securitycenter/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand All @@ -19,7 +20,7 @@ func TestAccSecurityCenterSubscriptionPricing_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_security_center_subscription_pricing", "test")
r := SecurityCenterSubscriptionPricingResource{}

//lintignore:AT001
// lintignore:AT001
data.ResourceSequentialTestSkipCheckDestroyed(t, []acceptance.TestStep{
{
Config: r.tier("Standard", "AppServices"),
Expand All @@ -40,18 +41,36 @@ func TestAccSecurityCenterSubscriptionPricing_update(t *testing.T) {
})
}

func TestAccSecurityCenterSubscriptionPricing_storageAccountSubplan(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_security_center_subscription_pricing", "test")
r := SecurityCenterSubscriptionPricingResource{}

// lintignore:AT001
data.ResourceSequentialTestSkipCheckDestroyed(t, []acceptance.TestStep{
{
Config: r.storageAccountSubplan(),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("tier").HasValue("Standard"),
check.That(data.ResourceName).Key("subplan").HasValue("PerStorageAccount"),
),
},
data.ImportStep(),
})
}

func (SecurityCenterSubscriptionPricingResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.PricingID(state.ID)
id, err := pricings_v2022_03_01.ParsePricingIDInsensitively(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.SecurityCenter.PricingClient.Get(ctx, id.Name)
resp, err := clients.SecurityCenter.PricingClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

return utils.Bool(resp.PricingProperties != nil), nil
return utils.Bool(resp.Model.Properties != nil), nil
}

func (SecurityCenterSubscriptionPricingResource) tier(tier string, resource_type string) string {
Expand All @@ -66,3 +85,17 @@ resource "azurerm_security_center_subscription_pricing" "test" {
}
`, tier, resource_type)
}

func (SecurityCenterSubscriptionPricingResource) storageAccountSubplan() string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_security_center_subscription_pricing" "test" {
tier = "Standard"
resource_type = "StorageAccounts"
subplan = "PerStorageAccount"
}
`)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading