-
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.
Fix error when creating security center contact
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. Fixes #8317
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
azurerm/internal/services/securitycenter/azuresdkhacks/security_center_contact.go
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,70 @@ | ||
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" | ||
"github.com/Azure/go-autorest/tracing" | ||
) | ||
|
||
// 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 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, nil | ||
} | ||
|
||
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 | ||
} |
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