Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/registry/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/stackitcloud/stackit-sdk-go/examples/registry

go 1.25

// This is not needed in production. This is only here to point the golangci linter to the local version instead of the last release on GitHub.
replace github.com/stackitcloud/stackit-sdk-go/services/registry => ../../services/registry

require (
github.com/stackitcloud/stackit-sdk-go/core v0.27.0
github.com/stackitcloud/stackit-sdk-go/services/registry v0.0.0
)

require (
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
github.com/google/uuid v1.6.0 // indirect
)
8 changes: 8 additions & 0 deletions examples/registry/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/stackitcloud/stackit-sdk-go/core v0.27.0 h1:7lc6qStcFDFf8zHP4ORPa9joybw+Sa2LtB6BVjKQ+ek=
github.com/stackitcloud/stackit-sdk-go/core v0.27.0/go.mod h1:WU1hhxnjXw2EV7CYa1nlEvNpMiRY6CvmIOaHuL3pOaA=
163 changes: 163 additions & 0 deletions examples/registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package main

import (
"context"
"fmt"
"os"

"github.com/stackitcloud/stackit-sdk-go/core/utils"
registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
"github.com/stackitcloud/stackit-sdk-go/services/registry/v1api/wait"
)

func main() {
projectId := "<project-uuid>" // the UUID of your STACKIT project
region := "eu01" // STACKIT region, e.g. "eu01"

// Create a new API client using default authentication and configuration
client, err := registry.NewAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err)
os.Exit(1)
}

// Alternatively, you can create a new API client using your own authentication and configuration
// Create a new API client, that will authenticate using the key flow
// If you created a service account key and provided your own RSA key pair,
// you need to add the path to a PEM encoded file including the private key
// using config.WithPrivateKeyPath("path/to/private_key.pem")
// saKeyPath := "/path/to/service_account_key.json"
// client, err := registry.NewAPIClient(
// config.WithServiceAccountKeyPath(saKeyPath), config.WithEndpoint("https://my-alternative-enpoint.com"),
// )
// if err != nil {
// fmt.Fprintf(os.Stderr, "[Registry] Creating API client: %v\n", err)
// os.Exit(1)
// }

ctx := context.Background()

// 1. List existing artifactories (registries) in the project
listResp, err := client.DefaultAPI.ListArtifactories(ctx, projectId, region).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error listing artifactories: %v\n", err)
} else {
fmt.Printf("Found %d artifactory(ies)\n", len(listResp.Artifactories))
for _, artifactory := range listResp.Artifactories {
fmt.Printf("- %s (ID: %s, State: %s)\n", artifactory.Name, artifactory.Id, artifactory.State)
}
}

// 2. Create a new artifactory
artifactoryName := "my-registry"
createPayload := registry.CreateArtifactoryPayload{
Name: artifactoryName,
}
createResp, err := client.DefaultAPI.CreateArtifactory(ctx, projectId, region).
CreateArtifactoryPayload(createPayload).
Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating artifactory: %v\n", err)
return
}

artifactoryId := createResp.Id
fmt.Printf("Created artifactory with ID %q. Waiting for it to become ready...\n", artifactoryId)

// 3. Wait for the artifactory to be ready
_, err = wait.CreateArtifactoryWaitHandler(ctx, client.DefaultAPI, projectId, region, artifactoryId).WaitWithContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error waiting for artifactory to be ready: %v\n", err)
return
}
fmt.Println("Artifactory is ready.")

// 4. Retrieve artifactory details
artifactory, err := client.DefaultAPI.GetArtifactory(ctx, projectId, region, artifactoryId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting artifactory details: %v\n", err)
return
}
fmt.Printf("Artifactory details: Name=%s, State=%s, Url=%s\n", artifactory.Name, artifactory.State, artifactory.Url)

// 5. Delete the artifactory
err = client.DefaultAPI.DeleteArtifactory(ctx, projectId, region, artifactoryId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting artifactory: %v\n", err)
return
}
fmt.Println("Artifactory deleted successfully.")

// ==========================================
// Proxy Artifactory Example
// ==========================================

// 6. Create a proxy artifactory (e.g. proxying Docker Hub)
proxyArtifactoryName := "my-proxy-registry2"
createProxyPayload := registry.CreateArtifactoryPayload{
Name: proxyArtifactoryName,
Proxy: &registry.CreateProxyRequest{
RegistryType: "docker-hub",
Url: "https://hub.docker.com",
Description: utils.Ptr("Docker Hub Proxy Cache"),
Credentials: &registry.Credentials{
AccessKey: utils.Ptr("my-proxy_access_key"),
AccessSecret: utils.Ptr("my-proxy_access_secret"),
},
},
}
createProxyResp, err := client.DefaultAPI.CreateArtifactory(ctx, projectId, region).
CreateArtifactoryPayload(createProxyPayload).
Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating proxy artifactory: %v\n", err)
return
}

proxyArtifactoryId := createProxyResp.Id
fmt.Printf("Created proxy artifactory with ID %q. Waiting for it to become ready...\n", proxyArtifactoryId)

// Wait for the proxy artifactory to be ready

_, err = wait.CreateArtifactoryWaitHandler(ctx, client.DefaultAPI, projectId, region, proxyArtifactoryId).WaitWithContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error waiting for proxy artifactory to be ready: %v\n", err)
return
}
fmt.Println("Proxy artifactory is ready.")

// 7. Update proxy credentials
patchPayload := registry.PatchArtifactoryPayload{
Proxy: &registry.UpdateProxyRequest{
Credentials: &registry.Credentials{
AccessKey: utils.Ptr("my-proxy_access_key_updated"),
AccessSecret: utils.Ptr("my-proxy_access_secret_updated"),
},
Description: utils.Ptr("Updated Docker Hub Proxy Cache"),
},
}
_, err = client.DefaultAPI.PatchArtifactory(ctx, projectId, region, proxyArtifactoryId).
PatchArtifactoryPayload(patchPayload).
Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error updating proxy credentials: %v\n", err)
return
}
fmt.Printf("Updating proxy credentials for artifactory %q. Waiting for update to complete...\n", proxyArtifactoryId)

// Wait for the update to complete
_, err = wait.UpdateArtifactoryWaitHandler(ctx, client.DefaultAPI, projectId, region, proxyArtifactoryId).WaitWithContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error waiting for proxy artifactory update: %v\n", err)
return
}
fmt.Println("Proxy artifactory credentials updated successfully.")

// 8. Delete the proxy artifactory
err = client.DefaultAPI.DeleteArtifactory(ctx, projectId, region, proxyArtifactoryId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error deleting proxy artifactory: %v\n", err)
return
}
fmt.Println("Proxy artifactory deleted successfully.")
}
2 changes: 2 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use (
./examples/postgresflex
./examples/rabbitmq
./examples/redis
./examples/registry
./examples/resourcemanager
./examples/runcommand
./examples/runtime
Expand Down Expand Up @@ -72,6 +73,7 @@ use (
./services/postgresflex
./services/rabbitmq
./services/redis
./services/registry
./services/resourcemanager
./services/runcommand
./services/sca
Expand Down
3 changes: 3 additions & 0 deletions services/registry/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v0.1.0
- **New:** SDK module for STACKIT Container Registry service.
- `v1`: New package which can be used for communication with the regsitry v1 API
Loading