Skip to content

Commit

Permalink
New Resource: azurerm_security_center_assessment_metadata (#10124)
Browse files Browse the repository at this point in the history
Currently, terraform doesn't support Security.AssessmentMetadata RP. So submitted this PR to implement it.

--- PASS: TestAccSecurityCenterAssessmentMetadata_basic (66.90s)
--- PASS: TestAccSecurityCenterAssessmentMetadata_complete (68.07s)
--- PASS: TestAccSecurityCenterAssessmentMetadata_update (125.80s)

The api in go sdk package:
https://github.com/Azure/azure-sdk-for-go/blob/master/services/preview/security/mgmt/v3.0/security/assessmentsmetadata.go
  • Loading branch information
Neil Ye authored Feb 22, 2021
1 parent b1fef30 commit afe0f3d
Show file tree
Hide file tree
Showing 11 changed files with 751 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/securitycenter/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

type Client struct {
AssessmentsMetadataClient *security.AssessmentsMetadataClient
ContactsClient *security.ContactsClient
DeviceSecurityGroupsClient *security.DeviceSecurityGroupsClient
IotSecuritySolutionClient *security.IotSecuritySolutionClient
Expand All @@ -20,6 +21,9 @@ type Client struct {
func NewClient(o *common.ClientOptions) *Client {
ascLocation := "Global"

AssessmentsMetadataClient := security.NewAssessmentsMetadataClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
o.ConfigureClient(&AssessmentsMetadataClient.Client, o.ResourceManagerAuthorizer)

ContactsClient := security.NewContactsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, ascLocation)
o.ConfigureClient(&ContactsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -48,6 +52,7 @@ func NewClient(o *common.ClientOptions) *Client {
o.ConfigureClient(&AutomationsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AssessmentsMetadataClient: &AssessmentsMetadataClient,
ContactsClient: &ContactsClient,
DeviceSecurityGroupsClient: &DeviceSecurityGroupsClient,
IotSecuritySolutionClient: &IotSecuritySolutionClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type AssessmentMetadataId struct {
SubscriptionId string
AssessmentMetadataName string
}

func NewAssessmentMetadataID(subscriptionId, assessmentMetadataName string) AssessmentMetadataId {
return AssessmentMetadataId{
SubscriptionId: subscriptionId,
AssessmentMetadataName: assessmentMetadataName,
}
}

func (id AssessmentMetadataId) String() string {
segments := []string{
fmt.Sprintf("Assessment Metadata Name %q", id.AssessmentMetadataName),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Assessment Metadata", segmentsStr)
}

func (id AssessmentMetadataId) ID() string {
fmtString := "/subscriptions/%s/providers/Microsoft.Security/assessmentMetadata/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.AssessmentMetadataName)
}

// AssessmentMetadataID parses a AssessmentMetadata ID into an AssessmentMetadataId struct
func AssessmentMetadataID(input string) (*AssessmentMetadataId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, err
}

resourceId := AssessmentMetadataId{
SubscriptionId: id.SubscriptionID,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.AssessmentMetadataName, err = id.PopSegment("assessmentMetadata"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid"
)

var _ resourceid.Formatter = AssessmentMetadataId{}

func TestAssessmentMetadataIDFormatter(t *testing.T) {
actual := NewAssessmentMetadataID("12345678-1234-9876-4563-123456789012", "metadata1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Security/assessmentMetadata/metadata1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestAssessmentMetadataID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *AssessmentMetadataId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing AssessmentMetadataName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Security/",
Error: true,
},

{
// missing value for AssessmentMetadataName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Security/assessmentMetadata/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Security/assessmentMetadata/metadata1",
Expected: &AssessmentMetadataId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
AssessmentMetadataName: "metadata1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/PROVIDERS/MICROSOFT.SECURITY/ASSESSMENTMETADATA/METADATA1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := AssessmentMetadataID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.AssessmentMetadataName != v.Expected.AssessmentMetadataName {
t.Fatalf("Expected %q but got %q for AssessmentMetadataName", v.Expected.AssessmentMetadataName, actual.AssessmentMetadataName)
}
}
}
1 change: 1 addition & 0 deletions azurerm/internal/services/securitycenter/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_iot_security_device_group": resourceIotSecurityDeviceGroup(),
"azurerm_iot_security_solution": resourceIotSecuritySolution(),
"azurerm_security_center_contact": resourceSecurityCenterContact(),
"azurerm_security_center_assessment_metadata": resourceArmSecurityCenterAssessmentMetadata(),
"azurerm_security_center_setting": resourceSecurityCenterSetting(),
"azurerm_security_center_subscription_pricing": resourceSecurityCenterSubscriptionPricing(),
"azurerm_security_center_workspace": resourceSecurityCenterWorkspace(),
Expand Down
1 change: 1 addition & 0 deletions azurerm/internal/services/securitycenter/resourceids.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package securitycenter

//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AssessmentMetadata -id=/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Security/assessmentMetadata/metadata1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=IotSecuritySolution -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Security/IoTSecuritySolutions/solution1
Loading

0 comments on commit afe0f3d

Please sign in to comment.