-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2072 from terraform-providers/r-security-workspace
New Resource: azurerm_securitycenter_workspace
- Loading branch information
Showing
10 changed files
with
447 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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,31 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAccAzureRMSecurityCenter_pricingAndWorkspace(t *testing.T) { | ||
// NOTE: this is a combined test rather than separate split out tests | ||
// due to the workspace tests depending on the current pricing tier | ||
testCases := map[string]map[string]func(t *testing.T){ | ||
"pricing": { | ||
"update": testAccAzureRMSecurityCenterSubscriptionPricing_update, | ||
}, | ||
"workspace": { | ||
"basic": testAccAzureRMSecurityCenterWorkspace_basic, | ||
"update": testAccAzureRMSecurityCenterWorkspace_update, | ||
}, | ||
} | ||
|
||
for group, m := range testCases { | ||
m := m | ||
t.Run(group, func(t *testing.T) { | ||
for name, tc := range m { | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
tc(t) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
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,161 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/2017-08-01-preview/security" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
//only valid name is default | ||
// Message="Invalid workspace settings name 'kttest' , only default is allowed " | ||
const securityCenterWorkspaceName = "default" | ||
|
||
func resourceArmSecurityCenterWorkspace() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmSecurityCenterWorkspaceCreateUpdate, | ||
Read: resourceArmSecurityCenterWorkspaceRead, | ||
Update: resourceArmSecurityCenterWorkspaceCreateUpdate, | ||
Delete: resourceArmSecurityCenterWorkspaceDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"scope": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
|
||
"workspace_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: azure.ValidateResourceID, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmSecurityCenterWorkspaceCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).securityCenterWorkspaceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
priceClient := meta.(*ArmClient).securityCenterPricingClient | ||
|
||
//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 | ||
price, err := priceClient.GetSubscriptionPricing(ctx, securityCenterSubscriptionPricingName) | ||
if err != nil { | ||
return fmt.Errorf("Error reading Security Center Subscription pricing: %+v", err) | ||
} | ||
|
||
if price.PricingProperties == nil { | ||
return fmt.Errorf("Security Center Subscription pricing propertier is nil") | ||
} | ||
if price.PricingProperties.PricingTier == security.Free { | ||
return fmt.Errorf("Security Center Subscription workspace cannot be set when pricing tier is `Free`") | ||
} | ||
|
||
name := securityCenterWorkspaceName | ||
|
||
contact := security.WorkspaceSetting{ | ||
WorkspaceSettingProperties: &security.WorkspaceSettingProperties{ | ||
Scope: utils.String(d.Get("scope").(string)), | ||
WorkspaceID: utils.String(d.Get("workspace_id").(string)), | ||
}, | ||
} | ||
|
||
if d.IsNewResource() { | ||
_, err := client.Create(ctx, name, contact) | ||
if err != nil { | ||
return fmt.Errorf("Error creating Security Center Workspace: %+v", err) | ||
} | ||
} else { | ||
_, err := client.Update(ctx, name, contact) | ||
if err != nil { | ||
return fmt.Errorf("Error updating Security Center Workspace: %+v", err) | ||
} | ||
} | ||
|
||
//api returns "" for workspace id after an create/update and eventually the new value | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"Waiting"}, | ||
Target: []string{"Populated"}, | ||
Timeout: 15 * time.Minute, | ||
MinTimeout: 30 * time.Second, | ||
Refresh: func() (interface{}, string, error) { | ||
|
||
resp, err := client.Get(ctx, name) | ||
if err != nil { | ||
return resp, "Error", fmt.Errorf("Error reading Security Center Workspace: %+v", err) | ||
} | ||
|
||
if properties := resp.WorkspaceSettingProperties; properties != nil { | ||
if properties.WorkspaceID != nil && *properties.WorkspaceID != "" { | ||
return resp, "Populated", nil | ||
} | ||
} | ||
|
||
return resp, "Waiting", nil | ||
}, | ||
} | ||
|
||
resp, err := stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("Error waiting: %+v", err) | ||
} | ||
|
||
if d.IsNewResource() { | ||
d.SetId(*resp.(security.WorkspaceSetting).ID) | ||
} | ||
|
||
return resourceArmSecurityCenterWorkspaceRead(d, meta) | ||
} | ||
|
||
func resourceArmSecurityCenterWorkspaceRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).securityCenterWorkspaceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
resp, err := client.Get(ctx, securityCenterWorkspaceName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Security Center Subscription Workspace was not found: %v", err) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error reading Security Center Workspace: %+v", err) | ||
} | ||
|
||
if properties := resp.WorkspaceSettingProperties; properties != nil { | ||
d.Set("scope", properties.Scope) | ||
d.Set("workspace_id", properties.WorkspaceID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmSecurityCenterWorkspaceDelete(_ *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).securityCenterWorkspaceClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
resp, err := client.Delete(ctx, securityCenterWorkspaceName) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp) { | ||
log.Printf("[DEBUG] Security Center Subscription Workspace was not found: %v", err) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error deleting Security Center Workspace: %+v", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.