yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/policy.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azure 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 22 "yunion.io/x/jsonutils" 23 "yunion.io/x/pkg/errors" 24 ) 25 26 type SPolicyDefinitonPropertieParameterMetadata struct { 27 DisplayName string 28 Description string 29 StrongType string 30 AssignPermissions bool 31 } 32 33 type SPolicyDefinitonPropertieParameter struct { 34 Type string 35 Metadata SPolicyDefinitonPropertieParameterMetadata 36 AllowedValues []string 37 DefaultValue []string 38 } 39 40 type SPolicyDefinitonProperties struct { 41 DisplayName string 42 PolicyType string 43 Mode string 44 Description string 45 Metadata SPolicyDefinitonPropertieMetadata 46 Parameters map[string]SPolicyDefinitonPropertieParameter 47 PolicyRule SPolicyDefinitonPropertieRule 48 } 49 50 type SPolicyDefinitonPropertieRuleThen struct { 51 Effect string 52 } 53 54 type SPolicyDefinitonPropertieRuleInfo jsonutils.JSONDict 55 56 type SPolicyDefinitonPropertieRule struct { 57 If jsonutils.JSONObject 58 Then SPolicyDefinitonPropertieRuleThen 59 } 60 61 type SPolicyDefinitonPropertieMetadata struct { 62 Version string 63 Category string 64 } 65 66 type SPolicyDefinition struct { 67 Properties SPolicyDefinitonProperties 68 Id string 69 Name string 70 Type string 71 } 72 73 func (client *SAzureClient) GetPolicyDefinitions() ([]SPolicyDefinition, error) { 74 definitions := []SPolicyDefinition{} 75 err := client.list("Microsoft.Authorization/policyDefinitions", url.Values{}, &definitions) 76 if err != nil { 77 return nil, errors.Wrap(err, "Microsoft.Authorization/policyDefinitions.List") 78 } 79 return definitions, nil 80 } 81 82 func (client *SAzureClient) GetPolicyDefinition(id string) (*SPolicyDefinition, error) { 83 definition := &SPolicyDefinition{} 84 err := client.get(id, url.Values{}, definition) 85 if err != nil { 86 return nil, errors.Wrapf(err, "get %s", id) 87 } 88 return definition, nil 89 } 90 91 type PolicyAssignmentPropertiesParameter struct { 92 Value []string 93 } 94 95 type PolicyAssignmentProperties struct { 96 DisplayName string 97 Parameters map[string]PolicyAssignmentPropertiesParameter 98 } 99 100 type SPolicyAssignment struct { 101 Id string 102 Properties PolicyAssignmentProperties 103 values []string 104 category string 105 condition string 106 parameters *jsonutils.JSONDict 107 } 108 109 func (assignment *SPolicyAssignment) GetName() string { 110 return assignment.Properties.DisplayName 111 } 112 113 func (assignment *SPolicyAssignment) GetGlobalId() string { 114 return strings.ToLower(assignment.Id) 115 } 116 117 func (assignment *SPolicyAssignment) GetCategory() string { 118 return assignment.category 119 } 120 121 func (assignment *SPolicyAssignment) GetCondition() string { 122 return assignment.condition 123 } 124 125 func (assignment *SPolicyAssignment) GetParameters() *jsonutils.JSONDict { 126 return assignment.parameters 127 } 128 129 func (client *SAzureClient) GetPolicyAssignments(defineId string) ([]SPolicyAssignment, error) { 130 assignments := []SPolicyAssignment{} 131 resource := "Microsoft.Authorization/policyAssignments" 132 params := url.Values{} 133 if len(defineId) > 0 { 134 params.Set("$filter", fmt.Sprintf(`policyDefinitionId eq '%s'`, defineId)) 135 } 136 err := client.list(resource, params, &assignments) 137 if err != nil { 138 return nil, errors.Wrap(err, "Microsoft.Authorization/policyAssignments.List") 139 } 140 return assignments, nil 141 }