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_contact - override SDK creation function to handle 201 response code #8774

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package azuresdkhacks

import (
"context"
"net/http"

"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"
)

func CreateSecurityCenterContact(client *security.ContactsClient, ctx context.Context, securityContactName string, securityContact security.Contact) (result security.Contact, err error) {
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 result, err
}

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
}

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

return result, nil
}

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())
if err != nil {
return result, err
}
result.Response = autorest.Response{Response: resp}
return result, nil
}
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,9 @@ func resourceArmSecurityCenterContactCreateUpdate(d *schema.ResourceData, meta i
}

if d.IsNewResource() {
if _, err := client.Create(ctx, name, contact); err != nil {
// TODO: switch back when the Swagger/API bug has been fixed:
// https://github.com/Azure/azure-rest-api-specs/issues/10717 (an undefined 201)
if _, err := azuresdkhacks.CreateSecurityCenterContact(client, ctx, name, contact); err != nil {
beandrad marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("Creating Security Center Contact: %+v", err)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/Azure/azure-sdk-for-go v46.4.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.10
github.com/Azure/go-autorest/autorest/date v0.3.0
github.com/Azure/go-autorest/autorest/validation v0.3.0
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
github.com/btubbs/datetime v0.1.0
github.com/davecgh/go-spew v1.1.1
github.com/google/uuid v1.1.1
Expand Down