Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (client *Client) GetAccountByName(name string) (*Account, error) {
opts := RequestOptions{
Path: "/admin/accounts",
Method: "GET",
QS: map[string]string{"filter[name]": name},
QS: map[string]string{"filter[name]": name},
}

resp, err := client.RequestAPI(&opts)
Expand All @@ -200,10 +200,10 @@ func (client *Client) GetAccountByName(name string) (*Account, error) {
}

var account *Account
for _, acc := range(accounts) {
for _, acc := range accounts {
if acc.Name == name {
account = &acc
}
}
}
if account == nil {
return nil, fmt.Errorf("GetAccountByName - cannot find account by name %s", name)
Expand Down Expand Up @@ -279,7 +279,7 @@ func (client *Client) CreateAccount(account *Account) (*Account, error) {
err = client.setAccountFeatures(account.Features, &respAccount)
if err != nil {
return nil, err
}
}
return &respAccount, nil
}

Expand Down Expand Up @@ -324,11 +324,11 @@ func (client *Client) UpdateAccount(account *Account) (*Account, error) {
if err != nil {
return nil, err
}

err = client.setAccountFeatures(account.Features, &respAccount)
if err != nil {
return nil, err
}
}

return &respAccount, nil
}
Expand Down Expand Up @@ -371,23 +371,23 @@ func GetAccountAdminsDiff(desiredAdmins []string, existingAdmins []string) (admi
}

// Update Features
func(client *Client) setAccountFeatures(features map[string]bool, account *Account) error {
func (client *Client) setAccountFeatures(features map[string]bool, account *Account) error {
id := account.GetID()
requestOptions := &RequestOptions{}
for k, v := range features{
for k, v := range features {
requestOptions.Body = []byte(fmt.Sprintf("{\"feature\": \"%s\"}", k))
if v {
requestOptions.Path = fmt.Sprintf("/features/%s", id)
requestOptions.Method = "POST"
} else {
requestOptions.Path = fmt.Sprintf("/features/switchOff/%s", id)
requestOptions.Method = "PUT"
}
requestOptions.Method = "PUT"
}
_, err := client.RequestAPI(requestOptions)
if err != nil {
return err
}
account.Features[k] = v
}
return nil
}
}
8 changes: 4 additions & 4 deletions client/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
userID, err = client.createRandomUser(accountId)
if err != nil {
return "", err
}
}
}
}
// login as user
xAccessToken, err = client.GetXAccessToken(userID, accountId)
if err != nil {
return "", err
}
}

// generate token
apiToken, err := client.GenerateToken(xAccessToken, apiKey)
Expand Down Expand Up @@ -261,4 +261,4 @@ func (client *Client) createRandomUser(accountId string) (string, error) {
}
return userID, nil

}
}
18 changes: 9 additions & 9 deletions client/current_account.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package client

import (
"fmt"
"encoding/json"
"fmt"
"github.com/stretchr/objx"
)

Expand All @@ -15,9 +15,9 @@ type CurrentAccountUser struct {

// CurrentAccount spec
type CurrentAccount struct {
ID string
Name string
Users []CurrentAccountUser
ID string
Name string
Users []CurrentAccountUser
}

// GetCurrentAccount -
Expand All @@ -32,7 +32,7 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
return nil, err
}
userRespStr := string(userResp)
currentAccountX, err := objx.FromJSON(userRespStr)
currentAccountX, err := objx.FromJSON(userRespStr)
if err != nil {
return nil, err
}
Expand All @@ -42,12 +42,12 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
return nil, fmt.Errorf("GetCurrentAccount - cannot get activeAccountName")
}
currentAccount := &CurrentAccount{
Name: activeAccountName,
Name: activeAccountName,
Users: make([]CurrentAccountUser, 0),
}

allAccountsI := currentAccountX.Get("account").InterSlice()
for _, accI := range(allAccountsI) {
for _, accI := range allAccountsI {
accX := objx.New(accI)
if accX.Get("name").String() == activeAccountName {
currentAccount.ID = accX.Get("id").String()
Expand All @@ -71,12 +71,12 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
if e := json.Unmarshal(accountUsersResp, &accountUsersI); e != nil {
return nil, fmt.Errorf("Cannot unmarshal accountUsers responce for accountId=%s: %v", currentAccount.ID, e)
}
for _, userI := range(accountUsersI) {
for _, userI := range accountUsersI {
userX := objx.New(userI)
userName := userX.Get("userName").String()
email := userX.Get("email").String()
userID := userX.Get("_id").String()
currentAccount.Users= append(currentAccount.Users, CurrentAccountUser{
currentAccount.Users = append(currentAccount.Users, CurrentAccountUser{
ID: userID,
UserName: userName,
Email: email,
Expand Down
42 changes: 21 additions & 21 deletions client/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (

// Permission spec
type Permission struct {
ID string `json:"id,omitempty"`
Team string `json:"role,omitempty"`
Resource string `json:"resource,omitempty"`
Action string `json:"action,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"attributes,omitempty"`
ID string `json:"id,omitempty"`
Team string `json:"role,omitempty"`
Resource string `json:"resource,omitempty"`
Action string `json:"action,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"attributes,omitempty"`
}

// NewPermission spec, diffs from Permission is `json:"team,omitempty"` vs `json:"role,omitempty"`
type NewPermission struct {
ID string `json:"_id,omitempty"`
Team string `json:"team,omitempty"`
Resource string `json:"resource,omitempty"`
Action string `json:"action,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"tags,omitempty"`
ID string `json:"_id,omitempty"`
Team string `json:"team,omitempty"`
Resource string `json:"resource,omitempty"`
Action string `json:"action,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"tags,omitempty"`
}

// GetPermissionList -
Expand Down Expand Up @@ -56,7 +56,7 @@ func (client *Client) GetPermissionList(teamID, action, resource string) ([]Perm
if resource != "" && p.Resource != resource {
continue
}
permissionsFiltered = append(permissionsFiltered, p)
permissionsFiltered = append(permissionsFiltered, p)
}

return permissionsFiltered, nil
Expand Down Expand Up @@ -85,15 +85,15 @@ func (client *Client) GetPermissionByID(id string) (*Permission, error) {
}

// CreatePermision -
func (client *Client) CreatePermission(permission *Permission) (*Permission, error) {
func (client *Client) CreatePermission(permission *Permission) (*Permission, error) {

newPermission := &NewPermission{
ID: permission.ID,
Team: permission.Team,
ID: permission.ID,
Team: permission.Team,
Resource: permission.Resource,
Action: permission.Action,
Account: permission.Account,
Tags: permission.Tags,
Action: permission.Action,
Account: permission.Account,
Tags: permission.Tags,
}

body, err := EncodeToJSON(newPermission)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (client *Client) CreatePermission(permission *Permission) (*Permission, er
}

newPermissionID := permissionResp[0].ID

return client.GetPermissionByID(newPermissionID)
}

Expand All @@ -144,4 +144,4 @@ func (client *Client) DeletePermission(id string) error {
}

return nil
}
}
9 changes: 4 additions & 5 deletions codefresh/data_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func dataSourceAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceAccountRead,
Read: dataSourceAccountRead,
Schema: map[string]*schema.Schema{
"_id": {
Type: schema.TypeString,
Expand All @@ -24,7 +24,7 @@ func dataSourceAccount() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
}
}
Expand All @@ -50,11 +50,11 @@ func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("data.codefresh_account - cannot find account")
}

return mapDataAccountToResource(account, d)
return mapDataAccountToResource(account, d)
}

func mapDataAccountToResource(account *cfClient.Account, d *schema.ResourceData) error {

if account == nil || account.ID == "" {
return fmt.Errorf("data.codefresh_account - failed to mapDataAccountToResource")
}
Expand All @@ -66,4 +66,3 @@ func mapDataAccountToResource(account *cfClient.Account, d *schema.ResourceData)

return nil
}

19 changes: 9 additions & 10 deletions codefresh/data_current_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func dataSourceCurrentAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceCurrentAccountRead,
Read: dataSourceCurrentAccountRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand All @@ -34,15 +34,14 @@ func dataSourceCurrentAccount() *schema.Resource {
"email": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
}
}


func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfClient.Client)
var currentAccount *cfClient.CurrentAccount
Expand All @@ -57,22 +56,22 @@ func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("data.codefresh_current_account - failed to get current_account")
}

return mapDataCurrentAccountToResource(currentAccount, d)
return mapDataCurrentAccountToResource(currentAccount, d)

}

func mapDataCurrentAccountToResource(currentAccount *cfClient.CurrentAccount, d *schema.ResourceData) error {

if currentAccount == nil || currentAccount.ID == "" {
return fmt.Errorf("data.codefresh_current_account - failed to mapDataCurrentAccountToResource")
}
d.SetId(currentAccount.ID)

d.Set("_id", currentAccount.ID)
d.Set("name", currentAccount.Name)

// users := make(map[string](map[string]interface{}))
// for n, user := range currentAccount.Users {
// for n, user := range currentAccount.Users {
// users[n] = make(map[string]interface{})
// users[n]["name"] = user.UserName
// users[n]["email"] = user.Email
Expand All @@ -87,7 +86,7 @@ func mapDataCurrentAccountToResource(currentAccount *cfClient.CurrentAccount, d
users[n]["email"] = user.Email
users[n]["id"] = user.ID
}
d.Set("users", users)

d.Set("users", users)
return nil
}
Loading