Skip to content

Commit

Permalink
Check whether Free tier really works
Browse files Browse the repository at this point in the history
  • Loading branch information
favoretti committed Oct 12, 2021
1 parent eb1a4d8 commit 06f966b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package securitycenter

import (
"context"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -55,7 +54,6 @@ func resourceSecurityCenterWorkspace() *pluginsdk.Resource {
}

func resourceSecurityCenterWorkspaceCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
priceClient := meta.(*clients.Client).SecurityCenter.PricingClient
client := meta.(*clients.Client).SecurityCenter.WorkspaceClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand All @@ -75,17 +73,6 @@ func resourceSecurityCenterWorkspaceCreateUpdate(d *pluginsdk.ResourceData, meta
}
}

// get pricing tier, workspace can only be configured when tier is not Free.
// API does not error, it just doesn't set the workspace scope
isPricingStandard, err := isPricingStandard(ctx, priceClient)
if err != nil {
return fmt.Errorf("Checking Security Center Subscription pricing tier %v", err)
}

if !isPricingStandard {
return fmt.Errorf("Security Center Subscription workspace cannot be set when pricing tier is `Free`")
}

workspaceID, err := parse.LogAnalyticsWorkspaceID(d.Get("workspace_id").(string))
if err != nil {
return err
Expand Down Expand Up @@ -145,27 +132,6 @@ func resourceSecurityCenterWorkspaceCreateUpdate(d *pluginsdk.ResourceData, meta
return resourceSecurityCenterWorkspaceRead(d, meta)
}

func isPricingStandard(ctx context.Context, priceClient *security.PricingsClient) (bool, error) {
prices, err := priceClient.List(ctx)
if err != nil {
return false, fmt.Errorf("Listing Security Center Subscription pricing: %+v", err)
}

if prices.Value != nil {
for _, resourcePrice := range *prices.Value {
if resourcePrice.PricingProperties == nil {
return false, fmt.Errorf("%v Security Center Subscription pricing properties is nil", *resourcePrice.Type)
}

if resourcePrice.PricingProperties.PricingTier == security.PricingTierStandard {
return true, nil
}
}
}

return false, nil
}

func resourceSecurityCenterWorkspaceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).SecurityCenter.WorkspaceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ provider "azurerm" {
}
resource "azurerm_security_center_subscription_pricing" "test" {
tier = "Standard"
tier = "Free"
resource_type = "VirtualMachines"
}
Expand Down Expand Up @@ -149,7 +149,7 @@ provider "azurerm" {
}
resource "azurerm_security_center_subscription_pricing" "test" {
tier = "Standard"
tier = "Free"
}
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit 06f966b

Please sign in to comment.