-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Mirantis/2202-user-resource
[WIP] PRODENG-2202 Adding User resource to provider
- Loading branch information
Showing
10 changed files
with
829 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,7 @@ jobs: | |
- uses: actions/[email protected] | ||
with: | ||
go-version-file: 'go.mod' | ||
cache: true | ||
- run: go mod download | ||
- run: go build -v . | ||
cache: false | ||
- name: Run linters | ||
uses: golangci/[email protected] | ||
with: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// CreateAccount struct. | ||
type CreateAccount struct { | ||
Name string `json:"name"` | ||
ID string `json:"id"` | ||
Password string `json:"password"` | ||
FullName string `json:"fullName,omitempty"` | ||
IsActive bool `json:"isActive,omitempty"` | ||
IsAdmin bool `json:"isAdmin,omitempty"` | ||
IsOrg bool `json:"isOrg,omitempty"` | ||
SearchLDAP bool `json:"searchLDAP,omitempty"` | ||
} | ||
|
||
// UpdateAccount struct. | ||
type UpdateAccount struct { | ||
FullName string `json:"fullName,omitempty"` | ||
IsActive bool `json:"isActive,omitempty"` | ||
IsAdmin bool `json:"isAdmin,omitempty"` | ||
} | ||
|
||
// ResponseAccount struct. | ||
type ResponseAccount struct { | ||
Name string `json:"name"` | ||
ID string `json:"id"` | ||
FullName string `json:"fullName,omitempty"` | ||
IsActive bool `json:"isActive"` | ||
IsAdmin bool `json:"isAdmin"` | ||
IsOrg bool `json:"isOrg"` | ||
IsImported bool `json:"isImported"` | ||
OnDemand bool `json:"onDemand"` | ||
OtpEnabled bool `json:"otpEnabled"` | ||
MembersCount int `json:"membersCount"` | ||
TeamsCount int `json:"teamsCount"` | ||
} | ||
|
||
// Account filters enum. | ||
type AccountFilter string | ||
|
||
const ( | ||
Users AccountFilter = "user" | ||
Orgs AccountFilter = "orgs" | ||
Admins AccountFilter = "admins" | ||
NonAdmins AccountFilter = "non-admins" | ||
ActiveUsers AccountFilter = "active-users" | ||
InactiveUsers AccountFilter = "inactive-users" | ||
URLTargetForAccounts = "accounts" | ||
) | ||
|
||
// APIFormOfFilter is a string readable form of the AccountFilters enum. | ||
func (accF AccountFilter) APIFormOfFilter() string { | ||
filters := [...]string{"users", "orgs", "admins", "non-admins", "active-users"} | ||
|
||
x := string(accF) | ||
for _, v := range filters { | ||
if v == x { | ||
return x | ||
} | ||
} | ||
|
||
return "all" | ||
} | ||
|
||
// CreateAccount method - checking the MKE health endpoint. | ||
func (c *Client) ApiCreateAccount(ctx context.Context, acc CreateAccount) (ResponseAccount, error) { | ||
if (acc == CreateAccount{}) { | ||
return ResponseAccount{}, fmt.Errorf("creating account failed. %w: %+v", ErrEmptyStruct, acc) | ||
} | ||
|
||
req, err := c.RequestFromTargetAndJSONBody(ctx, http.MethodPost, URLTargetForAccounts, acc) | ||
if err != nil { | ||
return ResponseAccount{}, fmt.Errorf("creating account %s failed. %w: %s", acc.Name, ErrRequestCreation, err) | ||
} | ||
|
||
resp, err := c.doAuthorizedRequest(req) | ||
if err != nil { | ||
return ResponseAccount{}, fmt.Errorf("creating account %s failed. %w", acc.Name, err) | ||
} | ||
|
||
resAcc := ResponseAccount{} | ||
if err := resp.JSONMarshallBody(&resAcc); err != nil { | ||
return ResponseAccount{}, fmt.Errorf("creating account %s failed. %w: %s", acc.Name, ErrUnmarshaling, err) | ||
} | ||
|
||
return resAcc, nil | ||
} | ||
|
||
// DeleteAccount deletes a user from in Enzi. | ||
func (c *Client) ApiDeleteAccount(ctx context.Context, id string) error { | ||
url := fmt.Sprintf("%s/%s", URLTargetForAccounts, id) | ||
req, err := c.RequestFromTargetAndBytesBody(ctx, http.MethodDelete, url, []byte{}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("deleting account %s failed. %w: %s", id, ErrRequestCreation, err) | ||
} | ||
|
||
if _, err = c.doAuthorizedRequest(req); err != nil { | ||
return fmt.Errorf("deleting account %s failed. %w", id, err) | ||
} | ||
return nil | ||
} | ||
|
||
// ReadAccount method retrieves a user from the enzi endpoint. | ||
func (c *Client) ApiReadAccount(ctx context.Context, id string) (ResponseAccount, error) { | ||
url := fmt.Sprintf("%s/%s", URLTargetForAccounts, id) | ||
req, err := c.RequestFromTargetAndBytesBody(ctx, http.MethodGet, url, []byte{}) | ||
if err != nil { | ||
return ResponseAccount{}, fmt.Errorf("reading account %s failed. %w: %s", id, ErrRequestCreation, err) | ||
} | ||
|
||
resp, err := c.doAuthorizedRequest(req) | ||
if err != nil { | ||
return ResponseAccount{}, fmt.Errorf("reading account %s failed. %w", id, err) | ||
} | ||
|
||
resAcc := ResponseAccount{} | ||
if err := resp.JSONMarshallBody(&resAcc); err != nil { | ||
return ResponseAccount{}, fmt.Errorf("reading account %s failed. %w: %s", id, ErrUnmarshaling, err) | ||
} | ||
return resAcc, nil | ||
} | ||
|
||
// UpdateAccount updates a user in the enzi endpoint. | ||
func (c *Client) ApiUpdateAccount(ctx context.Context, id string, acc UpdateAccount) (ResponseAccount, error) { | ||
url := fmt.Sprintf("%s/%s", URLTargetForAccounts, id) | ||
|
||
req, err := c.RequestFromTargetAndJSONBody(ctx, http.MethodPatch, url, acc) | ||
|
||
if err != nil { | ||
return ResponseAccount{}, fmt.Errorf("updating account %s failed. %w: %s", id, ErrRequestCreation, err) | ||
} | ||
|
||
resp, err := c.doAuthorizedRequest(req) | ||
if err != nil { | ||
return ResponseAccount{}, fmt.Errorf("updating account %s failed. %w", id, err) | ||
} | ||
|
||
resAcc := ResponseAccount{} | ||
if err := resp.JSONMarshallBody(&resAcc); err != nil { | ||
return ResponseAccount{}, fmt.Errorf("updating account %s failed. %w: %s", id, ErrUnmarshaling, err) | ||
} | ||
return resAcc, nil | ||
} | ||
|
||
// ReadAccounts method retrieves all accounts depending on the filter passed from the enzi endpoint. | ||
func (c *Client) ApiReadAccounts(ctx context.Context, accFilter AccountFilter) ([]ResponseAccount, error) { | ||
// req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.createEnziUrl("accounts"), nil) | ||
req, err := c.RequestFromTargetAndBytesBody(ctx, http.MethodGet, URLTargetForAccounts, []byte{}) | ||
if err != nil { | ||
return []ResponseAccount{}, fmt.Errorf("reading accounts in bulk '%s' failed. %w: %s", | ||
accFilter.APIFormOfFilter(), ErrRequestCreation, err) | ||
} | ||
|
||
q := req.URL.Query() | ||
q.Add("filter", accFilter.APIFormOfFilter()) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := c.doAuthorizedRequest(req) | ||
if err != nil { | ||
return []ResponseAccount{}, fmt.Errorf("reading accounts in bulk '%s' failed. %w", | ||
accFilter.APIFormOfFilter(), err) | ||
} | ||
|
||
accs := struct { | ||
UsersCount int `json:"usersCount"` | ||
OrgsCount int `json:"orgsCount"` | ||
ResourceCount int `json:"resourceCount"` | ||
NextPageStart string `json:"nextPageStart"` | ||
|
||
Accounts []ResponseAccount `json:"accounts"` | ||
}{} | ||
|
||
if err := resp.JSONMarshallBody(&accs); err != nil { | ||
return []ResponseAccount{}, fmt.Errorf("reading accounts in bulk '%s' failed. %w: %s", | ||
accFilter.APIFormOfFilter(), ErrUnmarshaling, err) | ||
} | ||
|
||
return accs.Accounts, nil | ||
} |
Oops, something went wrong.