func createArtifactRegistry()

in cmd/cloudshell_open/artifactregistry.go [29:74]


func createArtifactRegistry(project string, region string, repoName string) error {

	repoPrefix := fmt.Sprintf("projects/%s/locations/%s", project, region)
	repoFull := fmt.Sprintf("%s/repositories/%s", repoPrefix, repoName)

	ctx := context.Background()

	client, err := artifactregistry.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create artifact registry client: %w", err)
	}

	// Check for existing repo
	req := &artifactregistrypb.GetRepositoryRequest{
		Name: repoFull,
	}
	existingRepo, err := client.GetRepository(ctx, req)

	if err != nil {
		// The repo might not already exist, so allow that specific grpc error
		notFoundError := status.Error(codes.NotFound, "Requested entity was not found.")
		if !(errors.Is(err, notFoundError)) {
			return fmt.Errorf("failed to retrieve existing artifact registry client: %w", err)
		}
	}

	// If the existing repo doesn't exist, create it
	if existingRepo == nil {
		req := &artifactregistrypb.CreateRepositoryRequest{
			Parent:       repoPrefix,
			RepositoryId: repoName,
			Repository: &artifactregistrypb.Repository{
				Name:   repoFull,
				Format: artifactregistrypb.Repository_DOCKER,
			},
		}

		_, err := client.CreateRepository(context.TODO(), req)
		if err != nil {
			return fmt.Errorf("failed to create artifact registry: %w", err)
		}
	}

	return nil

}