|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +type EntityAbacAttribute struct { |
| 8 | + Name string `json:"name"` |
| 9 | + Key string `json:"key,omitempty"` |
| 10 | + Value string `json:"value"` |
| 11 | +} |
| 12 | + |
| 13 | +// GitopsAbacRule spec |
| 14 | +type GitopsAbacRule struct { |
| 15 | + ID string `json:"id,omitempty"` |
| 16 | + AccountId string `json:"accountId,omitempty"` |
| 17 | + EntityType string `json:"entityType"` |
| 18 | + Teams []string `json:"teams"` |
| 19 | + Tags []string `json:"tags,omitempty"` |
| 20 | + Actions []string `json:"actions"` |
| 21 | + Attributes []EntityAbacAttribute `json:"attributes"` |
| 22 | +} |
| 23 | + |
| 24 | +type GitopsAbacRulesListResponse struct { |
| 25 | + Data struct { |
| 26 | + AbacRules []GitopsAbacRule `json:"abacRules"` |
| 27 | + } `json:"data"` |
| 28 | +} |
| 29 | + |
| 30 | +type GitopsAbacRuleResponse struct { |
| 31 | + Data struct { |
| 32 | + AbacRule GitopsAbacRule `json:"abacRule,omitempty"` |
| 33 | + CreateAbacRule GitopsAbacRule `json:"createAbacRule,omitempty"` |
| 34 | + RemoveAbacRule GitopsAbacRule `json:"removeAbacRule,omitempty"` |
| 35 | + } `json:"data"` |
| 36 | +} |
| 37 | + |
| 38 | +func (client *Client) GetAbacRulesList(entityType string) ([]GitopsAbacRule, error) { |
| 39 | + request := GraphQLRequest{ |
| 40 | + Query: ` |
| 41 | + query AbacRules($accountId: String!, $entityType: AbacEntityValues!) { |
| 42 | + abacRules(accountId: $accountId, entityType: $entityType) { |
| 43 | + id |
| 44 | + accountId |
| 45 | + entityType |
| 46 | + teams |
| 47 | + tags |
| 48 | + actions |
| 49 | + attributes { |
| 50 | + name |
| 51 | + key |
| 52 | + value |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + `, |
| 57 | + Variables: map[string]interface{}{ |
| 58 | + "accountId": "", |
| 59 | + "entityType": entityType, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + response, err := client.SendGqlRequest(request) |
| 64 | + if err != nil { |
| 65 | + fmt.Println("Error:", err) |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + var gitopsAbacRulesResponse GitopsAbacRulesListResponse |
| 70 | + err = DecodeGraphQLResponseInto(response, &gitopsAbacRulesResponse) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + return gitopsAbacRulesResponse.Data.AbacRules, nil |
| 76 | +} |
| 77 | + |
| 78 | +// GetAbacRuleByID - |
| 79 | +func (client *Client) GetAbacRuleByID(id string) (*GitopsAbacRule, error) { |
| 80 | + request := GraphQLRequest{ |
| 81 | + Query: ` |
| 82 | + query AbacRule($accountId: String!, $id: ID!) { |
| 83 | + abacRule(accountId: $accountId, id: $id) { |
| 84 | + id |
| 85 | + accountId |
| 86 | + entityType |
| 87 | + teams |
| 88 | + tags |
| 89 | + actions |
| 90 | + attributes { |
| 91 | + name |
| 92 | + key |
| 93 | + value |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + `, |
| 98 | + Variables: map[string]interface{}{ |
| 99 | + "accountId": "", |
| 100 | + "id": id, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + response, err := client.SendGqlRequest(request) |
| 105 | + if err != nil { |
| 106 | + fmt.Println("Error:", err) |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + var gitopsAbacRuleResponse GitopsAbacRuleResponse |
| 111 | + err = DecodeGraphQLResponseInto(response, &gitopsAbacRuleResponse) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + return &gitopsAbacRuleResponse.Data.AbacRule, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (client *Client) CreateAbacRule(gitopsAbacRule *GitopsAbacRule) (*GitopsAbacRule, error) { |
| 120 | + |
| 121 | + newAbacRule := &GitopsAbacRule{ |
| 122 | + EntityType: gitopsAbacRule.EntityType, |
| 123 | + Teams: gitopsAbacRule.Teams, |
| 124 | + Tags: gitopsAbacRule.Tags, |
| 125 | + Actions: gitopsAbacRule.Actions, |
| 126 | + Attributes: gitopsAbacRule.Attributes, |
| 127 | + } |
| 128 | + |
| 129 | + request := GraphQLRequest{ |
| 130 | + Query: ` |
| 131 | + mutation CreateAbacRule($accountId: String!, $createAbacRuleInput: CreateAbacRuleInput!) { |
| 132 | + createAbacRule(accountId: $accountId, createAbacRuleInput: $createAbacRuleInput) { |
| 133 | + id |
| 134 | + accountId |
| 135 | + entityType |
| 136 | + teams |
| 137 | + tags |
| 138 | + actions |
| 139 | + attributes { |
| 140 | + name |
| 141 | + key |
| 142 | + value |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + `, |
| 147 | + Variables: map[string]interface{}{ |
| 148 | + "accountId": "", |
| 149 | + "createAbacRuleInput": newAbacRule, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + response, err := client.SendGqlRequest(request) |
| 154 | + if err != nil { |
| 155 | + fmt.Println("Error:", err) |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + var gitopsAbacRuleResponse GitopsAbacRuleResponse |
| 160 | + err = DecodeGraphQLResponseInto(response, &gitopsAbacRuleResponse) |
| 161 | + if err != nil { |
| 162 | + return nil, err |
| 163 | + } |
| 164 | + |
| 165 | + return &gitopsAbacRuleResponse.Data.CreateAbacRule, nil |
| 166 | +} |
| 167 | + |
| 168 | +func (client *Client) DeleteAbacRule(id string) (*GitopsAbacRule, error) { |
| 169 | + request := GraphQLRequest{ |
| 170 | + Query: ` |
| 171 | + mutation RemoveAbacRule($accountId: String!, $id: ID!) { |
| 172 | + removeAbacRule(accountId: $accountId, id: $id) { |
| 173 | + id |
| 174 | + accountId |
| 175 | + entityType |
| 176 | + teams |
| 177 | + tags |
| 178 | + actions |
| 179 | + attributes { |
| 180 | + name |
| 181 | + key |
| 182 | + value |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + `, |
| 187 | + Variables: map[string]interface{}{ |
| 188 | + "accountId": "", |
| 189 | + "id": id, |
| 190 | + }, |
| 191 | + } |
| 192 | + |
| 193 | + response, err := client.SendGqlRequest(request) |
| 194 | + if err != nil { |
| 195 | + fmt.Println("Error:", err) |
| 196 | + return nil, err |
| 197 | + } |
| 198 | + |
| 199 | + var gitopsAbacRuleResponse GitopsAbacRuleResponse |
| 200 | + err = DecodeGraphQLResponseInto(response, &gitopsAbacRuleResponse) |
| 201 | + if err != nil { |
| 202 | + return nil, err |
| 203 | + } |
| 204 | + |
| 205 | + return &gitopsAbacRuleResponse.Data.RemoveAbacRule, nil |
| 206 | +} |
0 commit comments