Skip to content

Commit

Permalink
Fix error when creating security center contact
Browse files Browse the repository at this point in the history
This allows to create a new security center contact.

The issue is that the Azure Rest API may return a 201 upon resource
creation, however the API specs don't include this status code as
successful, which causes the corresponding method from the Azure Go SDK
to return an error (see issue #8317).
  • Loading branch information
beandrad committed Oct 7, 2020
1 parent f6568c9 commit a804d83
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package azuresdkhacks

import (
"context"
"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/validation"
"github.com/Azure/go-autorest/tracing"
"net/http"
)

// The package's fully qualified name.
const fqdn = "github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/v3.0/security"

func CreateSecurityCenterContact(client *security.ContactsClient, ctx context.Context, securityContactName string, securityContact security.Contact) (result security.Contact, err error) {
if tracing.IsEnabled() {
ctx = tracing.StartSpan(ctx, fqdn+"/ContactsClient.Create")
defer func() {
sc := -1
if result.Response.Response != nil {
sc = result.Response.Response.StatusCode
}
tracing.EndSpan(ctx, sc, err)
}()
}
if err := validation.Validate([]validation.Validation{
{TargetValue: client.SubscriptionID,
Constraints: []validation.Constraint{{Target: "client.SubscriptionID", Name: validation.Pattern, Rule: `^[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}$`, Chain: nil}}},
{TargetValue: securityContact,
Constraints: []validation.Constraint{{Target: "securityContact.ContactProperties", Name: validation.Null, Rule: false,
Chain: []validation.Constraint{{Target: "securityContact.ContactProperties.Email", Name: validation.Null, Rule: true, Chain: nil}}}}}}); err != nil {
return result, validation.NewError("security.ContactsClient", "Create", err.Error())
}

req, err := client.CreatePreparer(ctx, securityContactName, securityContact)
if err != nil {
err = autorest.NewErrorWithError(err, "security.ContactsClient", "Create", nil, "Failure preparing request")
return
}

resp, err := client.CreateSender(req)
if err != nil {
result.Response = autorest.Response{Response: resp}
err = autorest.NewErrorWithError(err, "security.ContactsClient", "Create", resp, "Failure sending request")
return
}

result, err = createResponder(resp)
if err != nil {
err = autorest.NewErrorWithError(err, "security.ContactsClient", "Create", resp, "Failure responding to request")
}

return
}

func createResponder(resp *http.Response) (result security.Contact, err error) {
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
autorest.ByUnmarshallingJSON(&result),
autorest.ByClosing())
result.Response = autorest.Response{Response: resp}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/securitycenter/azuresdkhacks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -104,7 +105,7 @@ func resourceArmSecurityCenterContactCreateUpdate(d *schema.ResourceData, meta i
}

if d.IsNewResource() {
if _, err := client.Create(ctx, name, contact); err != nil {
if _, err := azuresdkhacks.CreateSecurityCenterContact(client, ctx, name, contact); err != nil {
return fmt.Errorf("Creating Security Center Contact: %+v", err)
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require (
github.com/Azure/go-autorest/autorest v0.10.0
github.com/Azure/go-autorest/autorest/azure/cli v0.3.1 // indirect
github.com/Azure/go-autorest/autorest/date v0.2.0
github.com/Azure/go-autorest/autorest/validation v0.2.0
github.com/Azure/go-autorest/tracing v0.5.0
github.com/btubbs/datetime v0.1.0
github.com/davecgh/go-spew v1.1.1
github.com/google/uuid v1.1.1
Expand Down

0 comments on commit a804d83

Please sign in to comment.