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

Add support for container registry operations #278

Merged
merged 19 commits into from
Nov 10, 2023

Conversation

optik-aper
Copy link
Member

@optik-aper optik-aper commented Nov 8, 2023

Description

Testing Instructions

Check that the functions return data from the API:

func main() {
	vultrClient, ctx := getClient()

	// VCR -- comment/un-comment each to test
	vcrGet(ctx, vultrClient)
	vcrList(ctx, vultrClient)
	vcrCreate(ctx, vultrClient)
	vcrUpdate(ctx, vultrClient)
	vcrDelete(ctx, vultrClient)
	vcrListRepos(ctx, vultrClient)
	vcrGetRepo(ctx, vultrClient)
	vcrDeleteRepo(ctx, vultrClient)
	vcrCreateDockerCredentials(ctx, vultrClient)
	vcrListRegions(ctx, vultrClient)
	vcrListPlans(ctx, vultrClient)
}

func getClient() (*govultr.Client, context.Context) {
	apiKey := "YOUR API KEY"
	config := &oauth2.Config{}
	ctx := context.Background()
	ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: apiKey})
	vultrClient := govultr.NewClient(oauth2.NewClient(ctx, ts))
	return vultrClient, ctx
}

func printJson(output interface{}) {

	json, err := json.MarshalIndent(output, "", "\t")

	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	fmt.Printf("%s", string(json))
}

func vcrGet(ctx context.Context, client *govultr.Client) {
	vcrID := "YOUR VCR ID"

	vcr, _, err := client.ContainerRegistry.Get(ctx, vcrID)
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcr)
}

func vcrList(ctx context.Context, client *govultr.Client) {
	vcrs, _, _, err := client.ContainerRegistry.List(ctx, nil)
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcrs)
}

func vcrListRegions(ctx context.Context, client *govultr.Client) {
	vcrRegions, _, _, err := client.ContainerRegistry.ListRegions(ctx, nil)
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcrRegions)
}

func vcrListPlans(ctx context.Context, client *govultr.Client) {
	vcrPlans, _, err := client.ContainerRegistry.ListPlans(ctx)
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcrPlans)
}

func vcrCreate(ctx context.Context, client *govultr.Client) {
	vcr, _, err := client.ContainerRegistry.Create(ctx, &govultr.ContainerRegistryReq{
		Name:   "govultrtest",
		Region: "sjc",
		Public: true,
		Plan:   "business",
	})
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcr)
}

func vcrUpdate(ctx context.Context, client *govultr.Client) {
	vcrID := "YOUR VCR ID"

	vcr, _, err := client.ContainerRegistry.Update(ctx, vcrID, &govultr.ContainerRegistryReqUpdate{
		Public: govultr.BoolToBoolPtr(true),
		Plan:   govultr.StringToStringPtr("business"),
	})
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcr)
}

func vcrDelete(ctx context.Context, client *govultr.Client) {
	vcrID := "YOUR VCR ID"

	if err := client.ContainerRegistry.Delete(ctx, vcrID); err != nil {
		fmt.Println(err)
		panic(err.Error())
	}
}

func vcrListRepos(ctx context.Context, client *govultr.Client) {
	vcrID := "YOUR VCR ID"

	vcrRepos, _, _, err := client.ContainerRegistry.ListRepositories(ctx, vcrID, nil)
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcrRepos)
}

func vcrGetRepo(ctx context.Context, client *govultr.Client) {
	vcrID := "YOUR VCR ID"
	repoName := "vultr-csi"

	vcrRepo, _, err := client.ContainerRegistry.GetRepository(ctx, vcrID, repoName)
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(vcrRepo)
}

func vcrCreateDockerCredentials(ctx context.Context, client *govultr.Client) {
	vcrID := "YOUR VCR ID"

	vcrCred, _, err := client.ContainerRegistry.CreateDockerCredentials(ctx, vcrID, &govultr.DockerCredentialsOpt{
		ExpirySeconds: govultr.IntToIntPtr(0),
		WriteAccess:   govultr.BoolToBoolPtr(false),
	})

	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	fmt.Print(vcrCred)
}

The tests:

go test -v -run TestVCRServiceHandler_                                                                                             20:37
=== RUN   TestVCRServiceHandler_Create
--- PASS: TestVCRServiceHandler_Create (0.00s)
=== RUN   TestVCRServiceHandler_List
--- PASS: TestVCRServiceHandler_List (0.00s)
=== RUN   TestVCRServiceHandler_Get
--- PASS: TestVCRServiceHandler_Get (0.00s)
=== RUN   TestVCRServiceHandler_Update
--- PASS: TestVCRServiceHandler_Update (0.00s)
=== RUN   TestVCRServiceHandler_Delete
--- PASS: TestVCRServiceHandler_Delete (0.00s)
=== RUN   TestVCRServiceHandler_GetRepository
--- PASS: TestVCRServiceHandler_GetRepository (0.00s)
=== RUN   TestVCRServiceHandler_ListRepositories
--- PASS: TestVCRServiceHandler_ListRepositories (0.00s)
=== RUN   TestVCRServiceHandler_UpdateRepository
--- PASS: TestVCRServiceHandler_UpdateRepository (0.00s)
=== RUN   TestVCRServiceHandler_DeleteRepository
--- PASS: TestVCRServiceHandler_DeleteRepository (0.00s)
=== RUN   TestVCRServiceHandler_ListRegions
--- PASS: TestVCRServiceHandler_ListRegions (0.00s)
=== RUN   TestVCRServiceHandler_ListPlans
--- PASS: TestVCRServiceHandler_ListPlans (0.00s)
PASS
ok      github.com/vultr/govultr/v3     0.007s

Checklist:

  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you linted your code locally prior to submission?
  • Have you successfully ran tests with your changes locally?

@optik-aper optik-aper self-assigned this Nov 8, 2023
@optik-aper optik-aper added the enhancement New feature or request label Nov 8, 2023
Copy link
Contributor

@christhemorse christhemorse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ready for prime time!

@optik-aper optik-aper merged commit b12163a into vultr:master Nov 10, 2023
6 checks passed
@optik-aper optik-aper mentioned this pull request Nov 10, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants