Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user resources for API v3 #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions enf/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type AuthRequest struct {
// Credentials represents the authentication credentials returned by
// the auth API.
type Credentials struct {
Username *string `json:"username"`
Token *string `json:"token"`
UserID *int64 `json:"user_id"`
UserType *string `json:"type"`
DomainID *int64 `json:"domain_id"`
DomainNetwork *string `json:"domain_network"`
Username *string `json:"username"`
Token *string `json:"token"`
UserID *int64 `json:"user_id"`
Roles []*UserRole `json:"roles"`
DomainID *int64 `json:"domain_id"`
Domain *string `json:"domain"`
}

type authResponse struct {
Expand All @@ -49,7 +49,7 @@ func (s *AuthService) Authenticate(ctx context.Context, authReq *AuthRequest) (*
return nil, nil, ErrMissingPassword
}

endpoint := "/api/xcr/v2/xauth"
endpoint := "/api/xcr/v3/xauth"

body, resp, err := s.client.post(ctx, endpoint, new(authResponse), authReq)
if err != nil {
Expand Down
19 changes: 15 additions & 4 deletions enf/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestAuthService_Authenticate(t *testing.T) {
path := "/api/xcr/v2/xauth"
path := "/api/xcr/v3/xauth"

requestBody := &AuthRequest{
Username: String("user"),
Expand All @@ -19,18 +19,29 @@ func TestAuthService_Authenticate(t *testing.T) {
{
"username":"user",
"token":"12345678",
"user_id":1
"user_id":1,
"roles": [
{
"cidr" : "N/n0",
"role" : "NETWORK_USER"
}
]
}
],
"page": {

}
}
}`

expected := &Credentials{
Username: String("user"),
Token: String("12345678"),
UserID: Int64(1),
Roles: []*UserRole{
{
CIDR: String("N/n0"),
Role: String("NETWORK_USER"),
},
},
}

method := func(client *Client) (interface{}, *http.Response, error) {
Expand Down
2 changes: 1 addition & 1 deletion enf/dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *DNSService) UpdateZone(ctx context.Context, zoneUUID string, req *Updat
// DeleteZone deletes a zone given its UUID.
func (s *DNSService) DeleteZone(ctx context.Context, zoneUUID string) (*http.Response, error) {
path := fmt.Sprintf("api/xdns/2019-05-27/zones/%v", zoneUUID)
resp, err := s.client.delete(ctx, path)
resp, err := s.client.delete(ctx, path, url.Values{})
if err != nil {
return resp, err
}
Expand Down
6 changes: 5 additions & 1 deletion enf/enf.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ func (c *Client) put(ctx context.Context, path string, body interface{}, fields
}

// delete makes delete requests to the given path.
func (c *Client) delete(ctx context.Context, path string) (*http.Response, error) {
func (c *Client) delete(ctx context.Context, path string, queryParameters url.Values) (*http.Response, error) {
req, err := c.NewRequest("DELETE", path, nil)
if err != nil {
return nil, err
}

// Add the query parameters to the request URL.
req.URL.RawQuery = queryParameters.Encode()

return c.Do(ctx, req, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion enf/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ func (s *FirewallService) CreateRule(ctx context.Context, network string, rule *
// DeleteRule deletes the firewall rule associated with the given network address and ID.
func (s *FirewallService) DeleteRule(ctx context.Context, network string, id string) (*http.Response, error) {
path := fmt.Sprintf("api/xfw/v1/%v/rule/%v", network, id)
return s.client.delete(ctx, path)
return s.client.delete(ctx, path, url.Values{})
}
71 changes: 51 additions & 20 deletions enf/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ type UserService service

// User represents an ENF user.
type User struct {
UserID *int `json:"user_id"`
Username *string `json:"username"`
Description *string `json:"description"`
FullName *string `json:"full_name"`
LastLogin *time.Time `json:"last_login"`
DomainID *int `json:"domain_id"`
Type *string `json:"type"`
ResetCode *string `json:"reset_code"`
ResetTime *time.Time `json:"reset_time"`
Status *string `json:"status"`
UserID *int `json:"user_id"`
Username *string `json:"username"`
Description *string `json:"description"`
FullName *string `json:"full_name"`
LastLogin *time.Time `json:"last_login"`
Domain *string `json:"domain"`
DomainID *int `json:"domain_id"`
Roles []*UserRole `json:"roles"`
ResetCode *string `json:"reset_code"`
ResetTime *time.Time `json:"reset_time"`
Status *string `json:"status"`
}

// UpdateUserStatusRequest represents the body of the request to update a user's status.
Expand All @@ -46,9 +47,10 @@ type userResponse struct {

type emptyResponse []interface{}

// ListUsersForDomainAddress gets the list of users for a given domain address.
func (s *UserService) ListUsersForDomainAddress(ctx context.Context, address string) ([]*User, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/domains/%v/users", address)
// ListUsers gets the list of all users.
func (s *UserService) ListUsers(ctx context.Context) ([]*User, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/users")

body, resp, err := s.client.get(ctx, path, url.Values{}, new(userResponse))
if err != nil {
return nil, resp, err
Expand All @@ -57,20 +59,49 @@ func (s *UserService) ListUsersForDomainAddress(ctx context.Context, address str
return body.(*userResponse).Data, resp, nil
}

// ListUsersForDomainID gets a list of users for a given unique domain identifier.
func (s *UserService) ListUsersForDomainID(ctx context.Context, id string) ([]*User, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/domains/%v/users", id)
body, resp, err := s.client.get(ctx, path, url.Values{}, new(userResponse))
// ListUsersForDomain gets the list of users with roles in a given domain.
func (s *UserService) ListUsersForDomain(ctx context.Context, domain string) ([]*User, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/users")

queryParameters := url.Values{}
queryParameters.Add("domain", domain)

body, resp, err := s.client.get(ctx, path, queryParameters, new(userResponse))
if err != nil {
return nil, resp, err
}

return body.(*userResponse).Data, resp, nil
}

// ListUsersForNetwork gets the list of users with roles in a given network.
func (s *UserService) ListUsersForNetwork(ctx context.Context, network string) ([]*User, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/users")

queryParameters := url.Values{}
queryParameters.Add("network", network)

body, resp, err := s.client.get(ctx, path, queryParameters, new(userResponse))
if err != nil {
return nil, resp, err
}

return body.(*userResponse).Data, resp, nil
}

func (s *UserService) GetUser(ctx context.Context, userID int) (*User, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/users/%v", userID)
body, resp, err := s.client.get(ctx, path, url.Values{}, new(userResponse))
if err != nil {
return nil, resp, err
}

return body.(*userResponse).Data[0], resp, nil
}

// UpdateUserStatus updates the status of a user to "ACTIVE" or "INACTIVE".
func (s *UserService) UpdateUserStatus(ctx context.Context, userID int, updateUserStatusRequest *UpdateUserStatusRequest) (*http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/users/%v/status", userID)
path := fmt.Sprintf("api/xcr/v3/users/%v/status", userID)
_, resp, err := s.client.put(ctx, path, new(emptyResponse), updateUserStatusRequest)
if err != nil {
return resp, err
Expand All @@ -81,7 +112,7 @@ func (s *UserService) UpdateUserStatus(ctx context.Context, userID int, updateUs

// EmailResetPasswordCode emails a reset password code to the given email address.
func (s *UserService) EmailResetPasswordCode(ctx context.Context, email string) (*http.Response, error) {
path := "api/xcr/v2/users/reset"
path := "api/xcr/v3/users/reset"

queryParameters := url.Values{}
queryParameters.Add("email", email)
Expand All @@ -95,7 +126,7 @@ func (s *UserService) EmailResetPasswordCode(ctx context.Context, email string)

// ResetPassword resets a user's password.
func (s *UserService) ResetPassword(ctx context.Context, resetPasswordRequest *ResetPasswordRequest) (*http.Response, error) {
path := "api/xcr/v2/users/reset"
path := "api/xcr/v3/users/reset"
_, resp, err := s.client.post(ctx, path, new(emptyResponse), resetPasswordRequest)
if err != nil {
return resp, err
Expand Down
38 changes: 20 additions & 18 deletions enf/user_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ type Invite struct {
ModifiedDate *time.Time `json:"modified_data"`
Version *int `json:"version"`
DomainID *int `json:"domain_id"`
Domain *string `json:"domain"`
Role *UserRole `json:"role"`
Email *string `json:"email"`
InviteToken *string `json:"invite_token"`
InvitedBy *string `json:"invited_by"`
Name *string `json:"name"`
Type *string `json:"type"`
}

// SendInviteRequest represents the body of the request to send an invite.
type SendInviteRequest struct {
Email *string `json:"email"`
FullName *string `json:"full_name"`
UserType *string `json:"user_type"`
Email *string `json:"email"`
FullName *string `json:"full_name"`
Domain *string `json:"domain"`
Roles []*UserRole `json:"roles"`
}

// AcceptInviteRequest represents the body of the request to accept an invite.
Expand All @@ -43,9 +45,9 @@ type inviteResponse struct {
Page map[string]interface{} `json:"page"`
}

// ListInvitesForDomainAddress gets a list of active invites for a given domain address.
func (s *UserService) ListInvitesForDomainAddress(ctx context.Context, address string) ([]*Invite, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/domains/%v/invites", address)
// ListInvites gets a list of active invites.
func (s *UserService) ListInvites(ctx context.Context) ([]*Invite, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/invites")
body, resp, err := s.client.get(ctx, path, url.Values{}, new(inviteResponse))
if err != nil {
return nil, resp, err
Expand All @@ -54,9 +56,9 @@ func (s *UserService) ListInvitesForDomainAddress(ctx context.Context, address s
return body.(*inviteResponse).Data, resp, nil
}

// SendNewInvite sends a new invite for a user to join the domain with the given address.
func (s *UserService) SendNewInvite(ctx context.Context, address string, inviteRequest *SendInviteRequest) (*Invite, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/domains/%v/invites", address)
// SendInvite sends an invite for a new user.
func (s *UserService) SendInvite(ctx context.Context, inviteRequest *SendInviteRequest) (*Invite, *http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/invites")
body, resp, err := s.client.post(ctx, path, new(inviteResponse), inviteRequest)
if err != nil {
return nil, resp, err
Expand All @@ -67,7 +69,7 @@ func (s *UserService) SendNewInvite(ctx context.Context, address string, inviteR

// AcceptInvite accepts an invite.
func (s *UserService) AcceptInvite(ctx context.Context, acceptInviteRequest *AcceptInviteRequest) (*http.Response, error) {
path := "api/xcr/v2/users/invites"
path := "api/xcr/v3/invites"
_, resp, err := s.client.post(ctx, path, new(emptyResponse), acceptInviteRequest)
if err != nil {
return resp, err
Expand All @@ -76,9 +78,9 @@ func (s *UserService) AcceptInvite(ctx context.Context, acceptInviteRequest *Acc
return resp, nil
}

// ResendInvite resends an invite to the given email address.
func (s *UserService) ResendInvite(ctx context.Context, email string) (*http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/invites/%v", email)
// ResendInvite resends the given invite.
func (s *UserService) ResendInvite(ctx context.Context, id int) (*http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/invites/%v", id)
_, resp, err := s.client.put(ctx, path, new(emptyResponse), struct{}{})
if err != nil {
return resp, err
Expand All @@ -87,10 +89,10 @@ func (s *UserService) ResendInvite(ctx context.Context, email string) (*http.Res
return resp, nil
}

// DeleteInvite deletes an invite to the given email address.
func (s *UserService) DeleteInvite(ctx context.Context, email string) (*http.Response, error) {
path := fmt.Sprintf("api/xcr/v2/invites/%v", email)
resp, err := s.client.delete(ctx, path)
// DeleteInvite deletes the given invite
func (s *UserService) DeleteInvite(ctx context.Context, id int) (*http.Response, error) {
path := fmt.Sprintf("api/xcr/v3/invites/%v", id)
resp, err := s.client.delete(ctx, path, url.Values{})
if err != nil {
return resp, err
}
Expand Down
Loading