-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## What - Add Gitops ABAC Rule resource ## Why For creating rules for gitops entities ## Notes ## Checklist * [x] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [x] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [x] _I have added tests, assuming new tests are warranted_. * [x] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
- Loading branch information
1 parent
2038cd8
commit ab97b85
Showing
18 changed files
with
832 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type EntityAbacAttribute struct { | ||
Name string `json:"name"` | ||
Key string `json:"key,omitempty"` | ||
Value string `json:"value"` | ||
} | ||
|
||
// GitopsAbacRule spec | ||
type GitopsAbacRule struct { | ||
ID string `json:"id,omitempty"` | ||
AccountId string `json:"accountId,omitempty"` | ||
EntityType string `json:"entityType"` | ||
Teams []string `json:"teams"` | ||
Tags []string `json:"tags,omitempty"` | ||
Actions []string `json:"actions"` | ||
Attributes []EntityAbacAttribute `json:"attributes"` | ||
} | ||
|
||
type GitopsAbacRulesListResponse struct { | ||
Data struct { | ||
AbacRules []GitopsAbacRule `json:"abacRules"` | ||
} `json:"data"` | ||
} | ||
|
||
type GitopsAbacRuleResponse struct { | ||
Data struct { | ||
AbacRule GitopsAbacRule `json:"abacRule,omitempty"` | ||
CreateAbacRule GitopsAbacRule `json:"createAbacRule,omitempty"` | ||
RemoveAbacRule GitopsAbacRule `json:"removeAbacRule,omitempty"` | ||
} `json:"data"` | ||
} | ||
|
||
func (client *Client) GetAbacRulesList(entityType string) ([]GitopsAbacRule, error) { | ||
request := GraphQLRequest{ | ||
Query: ` | ||
query AbacRules($accountId: String!, $entityType: AbacEntityValues!) { | ||
abacRules(accountId: $accountId, entityType: $entityType) { | ||
id | ||
accountId | ||
entityType | ||
teams | ||
tags | ||
actions | ||
attributes { | ||
name | ||
key | ||
value | ||
} | ||
} | ||
} | ||
`, | ||
Variables: map[string]interface{}{ | ||
"accountId": "", | ||
"entityType": entityType, | ||
}, | ||
} | ||
|
||
response, err := client.SendGqlRequest(request) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return nil, err | ||
} | ||
|
||
var gitopsAbacRulesResponse GitopsAbacRulesListResponse | ||
err = DecodeGraphQLResponseInto(response, &gitopsAbacRulesResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return gitopsAbacRulesResponse.Data.AbacRules, nil | ||
} | ||
|
||
// GetAbacRuleByID - | ||
func (client *Client) GetAbacRuleByID(id string) (*GitopsAbacRule, error) { | ||
request := GraphQLRequest{ | ||
Query: ` | ||
query AbacRule($accountId: String!, $id: ID!) { | ||
abacRule(accountId: $accountId, id: $id) { | ||
id | ||
accountId | ||
entityType | ||
teams | ||
tags | ||
actions | ||
attributes { | ||
name | ||
key | ||
value | ||
} | ||
} | ||
} | ||
`, | ||
Variables: map[string]interface{}{ | ||
"accountId": "", | ||
"id": id, | ||
}, | ||
} | ||
|
||
response, err := client.SendGqlRequest(request) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return nil, err | ||
} | ||
|
||
var gitopsAbacRuleResponse GitopsAbacRuleResponse | ||
err = DecodeGraphQLResponseInto(response, &gitopsAbacRuleResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &gitopsAbacRuleResponse.Data.AbacRule, nil | ||
} | ||
|
||
func (client *Client) CreateAbacRule(gitopsAbacRule *GitopsAbacRule) (*GitopsAbacRule, error) { | ||
|
||
newAbacRule := &GitopsAbacRule{ | ||
EntityType: gitopsAbacRule.EntityType, | ||
Teams: gitopsAbacRule.Teams, | ||
Tags: gitopsAbacRule.Tags, | ||
Actions: gitopsAbacRule.Actions, | ||
Attributes: gitopsAbacRule.Attributes, | ||
} | ||
|
||
request := GraphQLRequest{ | ||
Query: ` | ||
mutation CreateAbacRule($accountId: String!, $createAbacRuleInput: CreateAbacRuleInput!) { | ||
createAbacRule(accountId: $accountId, createAbacRuleInput: $createAbacRuleInput) { | ||
id | ||
accountId | ||
entityType | ||
teams | ||
tags | ||
actions | ||
attributes { | ||
name | ||
key | ||
value | ||
} | ||
} | ||
} | ||
`, | ||
Variables: map[string]interface{}{ | ||
"accountId": "", | ||
"createAbacRuleInput": newAbacRule, | ||
}, | ||
} | ||
|
||
response, err := client.SendGqlRequest(request) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return nil, err | ||
} | ||
|
||
var gitopsAbacRuleResponse GitopsAbacRuleResponse | ||
err = DecodeGraphQLResponseInto(response, &gitopsAbacRuleResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &gitopsAbacRuleResponse.Data.CreateAbacRule, nil | ||
} | ||
|
||
func (client *Client) DeleteAbacRule(id string) (*GitopsAbacRule, error) { | ||
request := GraphQLRequest{ | ||
Query: ` | ||
mutation RemoveAbacRule($accountId: String!, $id: ID!) { | ||
removeAbacRule(accountId: $accountId, id: $id) { | ||
id | ||
accountId | ||
entityType | ||
teams | ||
tags | ||
actions | ||
attributes { | ||
name | ||
key | ||
value | ||
} | ||
} | ||
} | ||
`, | ||
Variables: map[string]interface{}{ | ||
"accountId": "", | ||
"id": id, | ||
}, | ||
} | ||
|
||
response, err := client.SendGqlRequest(request) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return nil, err | ||
} | ||
|
||
var gitopsAbacRuleResponse GitopsAbacRuleResponse | ||
err = DecodeGraphQLResponseInto(response, &gitopsAbacRuleResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &gitopsAbacRuleResponse.Data.RemoveAbacRule, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// GraphQLRequest GraphQL query | ||
type GraphQLRequest struct { | ||
Query string `json:"query"` | ||
Variables map[string]interface{} `json:"variables,omitempty"` | ||
} | ||
|
||
func (client *Client) SendGqlRequest(request GraphQLRequest) ([]byte, error) { | ||
jsonRequest, err := json.Marshal(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", client.HostV2, bytes.NewBuffer(jsonRequest)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tokenHeader := client.TokenHeader | ||
if tokenHeader == "" { | ||
tokenHeader = "Authorization" | ||
} | ||
req.Header.Set(tokenHeader, client.Token) | ||
req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
|
||
httpClient := &http.Client{} | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode >= 400 { | ||
bodyBytes, _ := ioutil.ReadAll(resp.Body) | ||
return nil, errors.New(resp.Status + " " + string(bodyBytes)) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var buf bytes.Buffer | ||
_, err = buf.ReadFrom(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} | ||
|
||
func DecodeGraphQLResponseInto(body []byte, target interface{}) error { | ||
return json.Unmarshal(body, target) | ||
} |
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
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
Oops, something went wrong.