Skip to content

Commit

Permalink
Merge pull request #12 from Mirantis/2202-user-resource
Browse files Browse the repository at this point in the history
[WIP] PRODENG-2202 Adding User resource to provider
  • Loading branch information
james-nesbitt authored Aug 21, 2023
2 parents b0df979 + 2b58d39 commit 70bfe1d
Show file tree
Hide file tree
Showing 10 changed files with 829 additions and 18 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.3.4
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0
github.com/hashicorp/terraform-plugin-go v0.18.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.4.0
Expand All @@ -15,7 +16,6 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand Down Expand Up @@ -56,11 +56,9 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand Down
16 changes: 5 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0
github.com/Masterminds/sprig/v3 v3.2.1/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
Expand Down Expand Up @@ -85,6 +84,8 @@ github.com/hashicorp/terraform-plugin-docs v0.16.0 h1:UmxFr3AScl6Wged84jndJIfFcc
github.com/hashicorp/terraform-plugin-docs v0.16.0/go.mod h1:M3ZrlKBJAbPMtNOPwHicGi1c+hZUh7/g0ifT/z7TVfA=
github.com/hashicorp/terraform-plugin-framework v1.3.4 h1:dOTLsALgmQu+PawAvhfGQ04H0MeIz3EZmBw7OFvj7qs=
github.com/hashicorp/terraform-plugin-framework v1.3.4/go.mod h1:2gGDpWiTI0irr9NSTLFAKlTi6KwGti3AoU19rFqU30o=
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0 h1:DKb1bX7/EPZUTW6F5zdwJzS/EZ/ycVD6JAW5RYOj4f8=
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0/go.mod h1:dzxOiHh7O9CAwc6p8N4mR1H++LtRkl+u+21YNiBVNno=
github.com/hashicorp/terraform-plugin-go v0.18.0 h1:IwTkOS9cOW1ehLd/rG0y+u/TGLK9y6fGoBjXVUquzpE=
github.com/hashicorp/terraform-plugin-go v0.18.0/go.mod h1:l7VK+2u5Kf2y+A+742GX0ouLut3gttudmvMgN0PA74Y=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down Expand Up @@ -141,14 +142,12 @@ github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand All @@ -160,15 +159,12 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down Expand Up @@ -215,7 +211,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand All @@ -231,7 +226,6 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
185 changes: 185 additions & 0 deletions internal/client/api_account.go
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
}
Loading

0 comments on commit 70bfe1d

Please sign in to comment.