Skip to content

Commit

Permalink
Merge pull request nutanix#179 from nutanix/feat/users-rs-ds
Browse files Browse the repository at this point in the history
User Resource and Data Sources.
  • Loading branch information
marinsalinas authored Dec 3, 2020
2 parents 577cba9 + f723fe9 commit d21fbcd
Show file tree
Hide file tree
Showing 26 changed files with 3,687 additions and 77 deletions.
231 changes: 230 additions & 1 deletion client/v3/v3_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Service interface {
UpdateProject(uuid string, body *Project) (*Project, error)
DeleteProject(uuid string) error
CreateAccessControlPolicy(request *AccessControlPolicy) (*AccessControlPolicy, error)
GetAccessControlPolicy(uuid string) (*AccessControlPolicy, error)
GetAccessControlPolicy(accessControlPolicyUUID string) (*AccessControlPolicy, error)
ListAccessControlPolicy(getEntitiesRequest *DSMetadata) (*AccessControlPolicyListResponse, error)
ListAllAccessControlPolicy(filter string) (*AccessControlPolicyListResponse, error)
UpdateAccessControlPolicy(uuid string, body *AccessControlPolicy) (*AccessControlPolicy, error)
Expand All @@ -83,6 +83,15 @@ type Service interface {
ListAllRole(filter string) (*RoleListResponse, error)
UpdateRole(uuid string, body *Role) (*Role, error)
DeleteRole(uuid string) (*DeleteResponse, error)
CreateUser(request *UserIntentInput) (*UserIntentResponse, error)
GetUser(userUUID string) (*UserIntentResponse, error)
UpdateUser(uuid string, body *UserIntentInput) (*UserIntentResponse, error)
DeleteUser(uuid string) (*DeleteResponse, error)
ListUser(getEntitiesRequest *DSMetadata) (*UserListResponse, error)
ListAllUser(filter string) (*UserListResponse, error)
GetUserGroup(userUUID string) (*UserGroupIntentResponse, error)
ListUserGroup(getEntitiesRequest *DSMetadata) (*UserGroupListResponse, error)
ListAllUserGroup(filter string) (*UserGroupListResponse, error)
}

/*CreateVM Creates a VM
Expand Down Expand Up @@ -1598,3 +1607,223 @@ func (op Operations) DeleteRole(uuid string) (*DeleteResponse, error) {

return deleteResponse, op.client.Do(ctx, req, deleteResponse)
}

/*CreateUser creates a User
* This operation submits a request to create a userbased on the input parameters.
*
* @param request *VMIntentInput
* @return *UserIntentResponse
*/
func (op Operations) CreateUser(request *UserIntentInput) (*UserIntentResponse, error) {
ctx := context.TODO()

req, err := op.client.NewRequest(ctx, http.MethodPost, "/users", request)
if err != nil {
return nil, err
}

UserIntentResponse := new(UserIntentResponse)

return UserIntentResponse, op.client.Do(ctx, req, UserIntentResponse)
}

/*GetUser This operation gets a User.
*
* @param uuid The user uuid - string.
* @return *User
*/
func (op Operations) GetUser(userUUID string) (*UserIntentResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/users/%s", userUUID)
User := new(UserIntentResponse)

req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

return User, op.client.Do(ctx, req, User)
}

/*UpdateUser Updates a User
* This operation submits a request to update a existing User based on the input parameters
* @param uuid The uuid of the entity - string.
* @param body - *User
* @return *User, error
*/
func (op Operations) UpdateUser(uuid string, body *UserIntentInput) (*UserIntentResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/users/%s", uuid)
UserInput := new(UserIntentResponse)

req, err := op.client.NewRequest(ctx, http.MethodPut, path, body)
if err != nil {
return nil, err
}

return UserInput, op.client.Do(ctx, req, UserInput)
}

/*DeleteUser Deletes a User
* This operation submits a request to delete a existing User.
*
* @param uuid The uuid of the entity.
* @return void
*/
func (op Operations) DeleteUser(uuid string) (*DeleteResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/users/%s", uuid)

req, err := op.client.NewRequest(ctx, http.MethodDelete, path, nil)
deleteResponse := new(DeleteResponse)

if err != nil {
return nil, err
}

return deleteResponse, op.client.Do(ctx, req, deleteResponse)
}

/*ListUser gets a list of Users.
*
* @param metadata allows create filters to get specific data - *DSMetadata.
* @return *UserListResponse
*/
func (op Operations) ListUser(getEntitiesRequest *DSMetadata) (*UserListResponse, error) {
ctx := context.TODO()
path := "/users/list"

UserList := new(UserListResponse)

req, err := op.client.NewRequest(ctx, http.MethodPost, path, getEntitiesRequest)
if err != nil {
return nil, err
}

return UserList, op.client.Do(ctx, req, UserList)
}

// ListAllUser ...
func (op Operations) ListAllUser(filter string) (*UserListResponse, error) {
entities := make([]*UserIntentResponse, 0)

resp, err := op.ListUser(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("user"),
Length: utils.Int64Ptr(itemsPerPage),
})

if err != nil {
return nil, err
}

totalEntities := utils.Int64Value(resp.Metadata.TotalMatches)
remaining := totalEntities
offset := utils.Int64Value(resp.Metadata.Offset)

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListUser(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("user"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
})

if err != nil {
return nil, err
}

entities = append(entities, resp.Entities...)

offset += itemsPerPage
}

resp.Entities = entities
}

return resp, nil
}

/*GetUserGroup This operation gets a User.
*
* @param uuid The user uuid - string.
* @return *User
*/
func (op Operations) GetUserGroup(userGroupUUID string) (*UserGroupIntentResponse, error) {
ctx := context.TODO()

path := fmt.Sprintf("/user_groups/%s", userGroupUUID)
User := new(UserGroupIntentResponse)

req, err := op.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}

return User, op.client.Do(ctx, req, User)
}

/*ListUserGroup gets a list of UserGroups.
*
* @param metadata allows create filters to get specific data - *DSMetadata.
* @return *UserGroupListResponse
*/
func (op Operations) ListUserGroup(getEntitiesRequest *DSMetadata) (*UserGroupListResponse, error) {
ctx := context.TODO()
path := "/user_groups/list"

UserGroupList := new(UserGroupListResponse)

req, err := op.client.NewRequest(ctx, http.MethodPost, path, getEntitiesRequest)
if err != nil {
return nil, err
}

return UserGroupList, op.client.Do(ctx, req, UserGroupList)
}

// ListAllUserGroup ...
func (op Operations) ListAllUserGroup(filter string) (*UserGroupListResponse, error) {
entities := make([]*UserGroupIntentResponse, 0)

resp, err := op.ListUserGroup(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("user_group"),
Length: utils.Int64Ptr(itemsPerPage),
})

if err != nil {
return nil, err
}

totalEntities := utils.Int64Value(resp.Metadata.TotalMatches)
remaining := totalEntities
offset := utils.Int64Value(resp.Metadata.Offset)

if totalEntities > itemsPerPage {
for hasNext(&remaining) {
resp, err = op.ListUserGroup(&DSMetadata{
Filter: &filter,
Kind: utils.StringPtr("user"),
Length: utils.Int64Ptr(itemsPerPage),
Offset: utils.Int64Ptr(offset),
})

if err != nil {
return nil, err
}

entities = append(entities, resp.Entities...)

offset += itemsPerPage
}

resp.Entities = entities
}

return resp, nil
}
Loading

0 comments on commit d21fbcd

Please sign in to comment.