Skip to content

Commit

Permalink
Adding RequestDisallowedByPolicy cloud error (#2963)
Browse files Browse the repository at this point in the history
* Adding RequestDisallowedByPolicy cloud error

* Removing test content
  • Loading branch information
sankur-codes authored Jun 21, 2023
1 parent bcd60e8 commit 87f1e43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const (
CloudErrorCodeCannotDeleteLoadBalancerByID = "CannotDeleteLoadBalancerWithPrivateLinkService"
CloudErrorCodeInUseSubnetCannotBeDeleted = "InUseSubnetCannotBeDeleted"
CloudErrorCodeScopeLocked = "ScopeLocked"
CloudErrorCodeRequestDisallowedByPolicy = "RequestDisallowedByPolicy"
)

// NewCloudError returns a new CloudError
Expand Down
29 changes: 27 additions & 2 deletions pkg/cluster/deploybaseresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ func (m *manager) attachNSGs(ctx context.Context) error {

func (m *manager) setMasterSubnetPolicies(ctx context.Context) error {
// TODO: there is probably an undesirable race condition here - check if etags can help.
s, err := m.subnet.Get(ctx, m.doc.OpenShiftCluster.Properties.MasterProfile.SubnetID)
subnetId := m.doc.OpenShiftCluster.Properties.MasterProfile.SubnetID
s, err := m.subnet.Get(ctx, subnetId)
if err != nil {
return err
}
Expand All @@ -241,7 +242,31 @@ func (m *manager) setMasterSubnetPolicies(ctx context.Context) error {
}
s.SubnetPropertiesFormat.PrivateLinkServiceNetworkPolicies = to.StringPtr("Disabled")

return m.subnet.CreateOrUpdate(ctx, m.doc.OpenShiftCluster.Properties.MasterProfile.SubnetID, s)
err = m.subnet.CreateOrUpdate(ctx, subnetId, s)

if detailedErr, ok := err.(autorest.DetailedError); ok {
if strings.Contains(detailedErr.Original.Error(), "RequestDisallowedByPolicy") {
return &api.CloudError{
StatusCode: http.StatusBadRequest,
CloudErrorBody: &api.CloudErrorBody{
Code: api.CloudErrorCodeRequestDisallowedByPolicy,
Message: fmt.Sprintf("Resource %s was disallowed by policy.",
subnetId[strings.LastIndex(subnetId, "/")+1:],
),
Details: []api.CloudErrorBody{
{
Code: api.CloudErrorCodeRequestDisallowedByPolicy,
Message: fmt.Sprintf("Policy definition : %s\nPolicy Assignment : %s",
regexp.MustCompile(`policyDefinitionName":"([^"]+)"`).FindStringSubmatch(detailedErr.Original.Error())[1],
regexp.MustCompile(`policyAssignmentName":"([^"]+)"`).FindStringSubmatch(detailedErr.Original.Error())[1],
),
},
},
},
}
}
}
return err
}

// generateInfraID take base and returns a ID that
Expand Down

0 comments on commit 87f1e43

Please sign in to comment.