-
Notifications
You must be signed in to change notification settings - Fork 75
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
Standardize GetByName/FindbyName methods #211
Labels
enhancement
New feature or request
Comments
We have agreed on solution n.2 above, with the following enhancements:
Example of implementation // ReadUserByHref returns a user by its HREF, without need for
// searching in the adminOrg user list
func (adminOrg *AdminOrg) ReadUserByHref(href string) (*OrgUser, error) {
orgUser := NewUser(adminOrg.client, adminOrg)
_, err := adminOrg.client.ExecuteRequest(href, http.MethodGet,
types.MimeAdminUser, "error getting user: %s", nil, orgUser.User)
if err != nil {
return nil, err
}
return orgUser, nil
}
// ReadUserByName retrieves a user within an admin organization by name
// Returns a valid user if it exists. If it doesn't, returns nil and ErrorEntityNotFound
func (adminOrg *AdminOrg) ReadUserByName(name string, refresh bool) (*OrgUser, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, user := range adminOrg.AdminOrg.Users.User {
if user.Name == name {
return adminOrg.ReadUserByHref(user.HREF)
}
}
return nil, ErrorEntityNotFound
}
// ReadUserById retrieves a user within an admin organization by ID
// Returns a valid user if it exists. If it doesn't, returns nil and ErrorEntityNotFound
func (adminOrg *AdminOrg) ReadUserById(id string, refresh bool) (*OrgUser, error) {
if refresh {
err := adminOrg.Refresh()
if err != nil {
return nil, err
}
}
for _, user := range adminOrg.AdminOrg.Users.User {
if user.ID == id {
return adminOrg.ReadUserByHref(user.HREF)
}
}
return nil, ErrorEntityNotFound
}
// ReadUserByNameOrId retrieves a user within an admin organization
// by either name or ID
// Returns a valid user if it exists. If it doesn't, returns nil and ErrorEntityNotFound
func (adminOrg *AdminOrg) ReadUserByNameOrId(identifier string, refresh bool) (*OrgUser, error) {
// First look by ID
orgUser, err := adminOrg.ReadUserByName(identifier, true)
// if it fails, look by name
if IsNotFound(err) {
orgUser, err = adminOrg.ReadUserById(identifier, false)
}
return orgUser, err
} |
dataclouder
added a commit
that referenced
this issue
Jul 5, 2019
* Update AdminOrg structure to include roles and users * Define the new type User * Define auxiliary types RoleReference, RightReference * Add AdminOrg methods GetRole, FetchUserByHref * Add AdminOrg methods FetchUserByName, FetchUserById, FetchUserByNameOrId * Add AdminOrg methods CreateUser, CreateUserSimple * Add OrgUser methods GetRoleName, Update, Enable, Disable * Add OrgUser methods Delete * Add OrgUser methods ChangePassword, Unlock, TakeOwnership * Add tests for org user retrieval, creation, update, deletion * Make sure that System Admin and Org Admin can run these functions Implement recommendations from Issue #211
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB: This issue is relevant to the implementation of data sources/imports in terraform-provider-vcd.
Problem
Currently we have several methods with the format
We have three problems with these methods:
Find...
or allGet...
Entity
structure, some a pointer to the entityProblem n.2 and n.3 contribute to complicate usage of such methods. Currently we do something like:
The point is that we don't know for sure what the error means, and we don't know what to expect from the return value.
Proposal
To alleviate the problems above, we need to
Solution 1: remove the error and use pointers
If we want the users to inspect the returned object to determine whether the search was successful, we don't need the error. We just return a pointer to the structure and the receiver function can simply say
if entity != nil
and that's it.Solution 2: Give meaning to the error and return pointers
Using this method, the receiving function will do:
Standardization
Whichever solution we choose, we should do six things:
FetchEntityByNameOrId
I vote for Solution n. 2
CC: @lvirbalas @vbauzysvmware @Didainius
The text was updated successfully, but these errors were encountered: