-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAPI based User management (#745)
- Loading branch information
Showing
6 changed files
with
254 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* Added types `OpenApiUser` and `types.OpenApiUser` for managing Org Users using OpenAPI | ||
`VCDClient.CreateUser`, `VCDClient.GetAllUsers`, `VCDClient.GetUserByName`, | ||
`VCDClient.GetUserById`, `OpenApiUser.Update`, `OpenApiUser.Delete` [GH-745] |
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,111 @@ | ||
package govcd | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/vmware/go-vcloud-director/v3/types/v56" | ||
) | ||
|
||
const labelOpenApiUser = "User" | ||
|
||
type OpenApiUser struct { | ||
User *types.OpenApiUser | ||
vcdClient *VCDClient | ||
TenantContext *TenantContext | ||
} | ||
|
||
// wrap is a hidden helper that facilitates the usage of a generic CRUD function | ||
// | ||
//lint:ignore U1000 this method is used in generic functions, but annoys staticcheck | ||
func (g OpenApiUser) wrap(inner *types.OpenApiUser) *OpenApiUser { | ||
g.User = inner | ||
return &g | ||
} | ||
|
||
// CreateUser creates a new User with a given configuration | ||
func (vcdClient *VCDClient) CreateUser(config *types.OpenApiUser, ctx *TenantContext) (*OpenApiUser, error) { | ||
c := crudConfig{ | ||
entityLabel: labelOpenApiUser, | ||
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointUsers, | ||
additionalHeader: getTenantContextHeader(ctx), | ||
requiresTm: true, | ||
} | ||
outerType := OpenApiUser{vcdClient: vcdClient, TenantContext: ctx} | ||
return createOuterEntity(&vcdClient.Client, outerType, c, config) | ||
} | ||
|
||
// GetAllUsers retrieves all Users with an optional filter | ||
func (vcdClient *VCDClient) GetAllUsers(queryParameters url.Values, ctx *TenantContext) ([]*OpenApiUser, error) { | ||
c := crudConfig{ | ||
entityLabel: labelOpenApiUser, | ||
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointUsers, | ||
queryParameters: queryParameters, | ||
additionalHeader: getTenantContextHeader(ctx), | ||
requiresTm: true, | ||
} | ||
|
||
outerType := OpenApiUser{vcdClient: vcdClient, TenantContext: ctx} | ||
return getAllOuterEntities(&vcdClient.Client, outerType, c) | ||
} | ||
|
||
// GetUserByName retrieves User by Name | ||
func (vcdClient *VCDClient) GetUserByName(username string, ctx *TenantContext) (*OpenApiUser, error) { | ||
if username == "" { | ||
return nil, fmt.Errorf("%s lookup requires username", labelOpenApiUser) | ||
} | ||
|
||
queryParams := url.Values{} | ||
queryParams.Add("filter", "username=="+username) | ||
|
||
filteredEntities, err := vcdClient.GetAllUsers(queryParams, ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
singleEntity, err := oneOrError("username", username, filteredEntities) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return vcdClient.GetUserById(singleEntity.User.ID, ctx) | ||
} | ||
|
||
// GetUserById retrieves User by ID | ||
func (vcdClient *VCDClient) GetUserById(id string, ctx *TenantContext) (*OpenApiUser, error) { | ||
c := crudConfig{ | ||
entityLabel: labelOpenApiUser, | ||
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointUsers, | ||
endpointParams: []string{id}, | ||
additionalHeader: getTenantContextHeader(ctx), | ||
requiresTm: true, | ||
} | ||
|
||
outerType := OpenApiUser{vcdClient: vcdClient, TenantContext: ctx} | ||
return getOuterEntity(&vcdClient.Client, outerType, c) | ||
} | ||
|
||
// Update User with a given config | ||
func (o *OpenApiUser) Update(cfg *types.OpenApiUser) (*OpenApiUser, error) { | ||
c := crudConfig{ | ||
entityLabel: labelOpenApiUser, | ||
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointUsers, | ||
endpointParams: []string{o.User.ID}, | ||
additionalHeader: getTenantContextHeader(o.TenantContext), | ||
requiresTm: true, | ||
} | ||
outerType := OpenApiUser{vcdClient: o.vcdClient} | ||
return updateOuterEntity(&o.vcdClient.Client, outerType, c, cfg) | ||
} | ||
|
||
// Delete User | ||
func (o *OpenApiUser) Delete() error { | ||
c := crudConfig{ | ||
entityLabel: labelOpenApiUser, | ||
endpoint: types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointUsers, | ||
endpointParams: []string{o.User.ID}, | ||
additionalHeader: getTenantContextHeader(o.TenantContext), | ||
requiresTm: true, | ||
} | ||
return deleteEntityById(&o.vcdClient.Client, c) | ||
} |
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,93 @@ | ||
//go:build tm || functional || ALL | ||
|
||
/* | ||
* Copyright 2025 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. | ||
*/ | ||
|
||
package govcd | ||
|
||
import ( | ||
"github.com/vmware/go-vcloud-director/v3/types/v56" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
func (vcd *TestVCD) Test_TmOpenApiUserLocal(check *C) { | ||
skipNonTm(vcd, check) | ||
sysadminOnly(vcd, check) | ||
|
||
org, orgCleanup := createOrg(vcd, check, false) | ||
defer orgCleanup() | ||
|
||
// TODO: TM: Change to vcdClient.GetTmOrgById(orgId), requires implementing Role support for that type | ||
adminOrg, err := vcd.client.GetAdminOrgById(org.TmOrg.ID) | ||
check.Assert(err, IsNil) | ||
check.Assert(adminOrg, NotNil) | ||
|
||
roleOrgAdmin, err := adminOrg.GetRoleByName("Organization Administrator") | ||
check.Assert(err, IsNil) | ||
check.Assert(roleOrgAdmin, NotNil) | ||
|
||
roleOrgUser, err := adminOrg.GetRoleByName("Organization User") | ||
check.Assert(err, IsNil) | ||
check.Assert(roleOrgUser, NotNil) | ||
|
||
userConfig := &types.OpenApiUser{ | ||
Username: "test-user", | ||
Password: "CHANGE-ME", | ||
RoleEntityRefs: []types.OpenApiReference{{ID: roleOrgAdmin.Role.ID}}, | ||
ProviderType: "LOCAL", | ||
OrgEntityRef: &types.OpenApiReference{ID: org.TmOrg.ID, Name: org.TmOrg.Name}, | ||
} | ||
|
||
tenantContext := &TenantContext{ | ||
OrgId: org.TmOrg.ID, | ||
OrgName: org.TmOrg.Name, | ||
} | ||
|
||
// Create | ||
createdUser, err := vcd.client.CreateUser(userConfig, tenantContext) | ||
check.Assert(err, IsNil) | ||
check.Assert(createdUser, NotNil) | ||
check.Assert(createdUser.User, NotNil) | ||
|
||
// GetAll | ||
allUsers, err := vcd.client.GetAllUsers(nil, tenantContext) | ||
check.Assert(err, IsNil) | ||
check.Assert(len(allUsers), Equals, 1) | ||
check.Assert(allUsers[0].User.ID, Equals, createdUser.User.ID) | ||
|
||
// Get by Username | ||
byUsername, err := vcd.client.GetUserByName(userConfig.Username, tenantContext) | ||
check.Assert(err, IsNil) | ||
check.Assert(byUsername.User.ID, Equals, createdUser.User.ID) | ||
|
||
// Get by ID | ||
byId, err := vcd.client.GetUserById(createdUser.User.ID, tenantContext) | ||
check.Assert(err, IsNil) | ||
check.Assert(byId.User.ID, Equals, createdUser.User.ID) | ||
|
||
// Update | ||
updateConfig := &types.OpenApiUser{ | ||
ID: byId.User.ID, | ||
Username: "test-user-updated", | ||
Password: "CHANGE-ME-UPDATED", | ||
RoleEntityRefs: []types.OpenApiReference{{ID: roleOrgUser.Role.ID}}, | ||
ProviderType: "LOCAL", | ||
NameInSource: userConfig.Username, // previous username must be provided | ||
OrgEntityRef: &types.OpenApiReference{ID: org.TmOrg.ID, Name: org.TmOrg.Name}, | ||
} | ||
|
||
updatedUser, err := byId.Update(updateConfig) | ||
check.Assert(err, IsNil) | ||
check.Assert(updatedUser, NotNil) | ||
check.Assert(updatedUser.User.ID, Equals, createdUser.User.ID) | ||
|
||
// Delete | ||
err = updatedUser.Delete() | ||
check.Assert(err, IsNil) | ||
|
||
// Get by ID and fail | ||
byIdNotFound, err := vcd.client.GetUserById(updatedUser.User.ID, tenantContext) | ||
check.Assert(ContainsNotFound(err), Equals, true) | ||
check.Assert(byIdNotFound, IsNil) | ||
} |
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