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

v2: Get by name or ID #2959

Open
vooon opened this issue Feb 29, 2024 · 6 comments
Open

v2: Get by name or ID #2959

vooon opened this issue Feb 29, 2024 · 6 comments

Comments

@vooon
Copy link
Contributor

vooon commented Feb 29, 2024

Pretty often i need similar to python library calls where you get resource by name or ID.
So i think it will be nice to upstream my utility helpers.

Unsure how good it fits gophercloud API though...

A code like that:

func (m *OpenstackManager) GetServer(ctx context.Context, nameOrID string) (*servers.Server, error) {
	ctx, span := tracer.Start(ctx, "ostack/GetServer", trace.WithSpanKind(trace.SpanKindClient))
	defer span.End()

	_, err := uuid.Parse(nameOrID)
	if err == nil {
		srv, err := servers.Get(ctx, m.computeCli, nameOrID).Extract()
		if err != nil {
			span.RecordError(err)
			return nil, err
		}
		return srv, nil
	}

	page, err := servers.List(m.computeCli, servers.ListOpts{Name: nameOrID}).AllPages(ctx)
	if err != nil {
		span.RecordError(err)
		return nil, err
	}

	srvs, err := servers.ExtractServers(page)
	if err != nil {
		span.RecordError(err)
		return nil, err
	}

	if len(srvs) == 0 {
		err = fmt.Errorf("No servers with name: %s", nameOrID)
		span.RecordError(err)
		return nil, err
	}

	return &srvs[0], nil
}

(well, i'm not proud of that UUID usage, but that good-enough ad-hoc solution...)

@pierreprinetti
Copy link
Contributor

we have that sort of functionality in utils at the moment: https://github.com/gophercloud/utils/blob/master/openstack/compute/v2/servers/utils.go

I would not be opposed to having some clever generic replacement for that in the main library.

@vooon
Copy link
Contributor Author

vooon commented Feb 29, 2024

@pierreprinetti well, the solution is not that clever, just if something looks like the UUID, it's probably UUID.

Comparing to IDsFromName that assumption saves us a call and useless unmarshal.
To be honest i don't like the speed of most of OpenStack listing APIs.

How should we name that sort of functions?

@pierreprinetti
Copy link
Contributor

I would be opposed to adding to Gophercloud a function that accepts a string that can equally be uuid or name. I’d let that guesswork to the caller application

@vooon
Copy link
Contributor Author

vooon commented Feb 29, 2024

@pierreprinetti and that guesswork already handled by my apps. Unfortunately i cannot expose my proprietary common code, but that's a pain, when i work with a things i'm allowed to opensource...

@pierreprinetti
Copy link
Contributor

On the other hand, goohercloud/utils was created specifically to host this kind of code 😁

@vooon
Copy link
Contributor Author

vooon commented Feb 29, 2024

Well, i actually tried to drop dependencies on utils... :)

But can you please suggest me a name for that kind of functions?
Behaves like Get, but it's more than that. GetBy looks too short and non intuitive, but GetByNameOrID feels to long and verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants