|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package snippets |
| 16 | + |
| 17 | +// [START privateca_create_ca_pool] |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + |
| 23 | + privateca "cloud.google.com/go/security/privateca/apiv1" |
| 24 | + "cloud.google.com/go/security/privateca/apiv1/privatecapb" |
| 25 | +) |
| 26 | + |
| 27 | +// Create a Certificate Authority pool. All certificates created under this CA pool will |
| 28 | +// follow the same issuance policy, IAM policies, etc. |
| 29 | +func createCaPool(w io.Writer, projectID string, location string, caPoolId string) error { |
| 30 | + // projectID := "your_project_id" |
| 31 | + // location := "us-central1" // For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations. |
| 32 | + // caPoolId := "ca-pool-id" // A unique id/name for the ca pool. |
| 33 | + |
| 34 | + ctx := context.Background() |
| 35 | + caClient, err := privateca.NewCertificateAuthorityClient(ctx) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("NewCertificateAuthorityClient creation failed: %w", err) |
| 38 | + } |
| 39 | + defer caClient.Close() |
| 40 | + |
| 41 | + caPool := &privatecapb.CaPool{ |
| 42 | + // Set the tier (see: https://cloud.google.com/certificate-authority-service/docs/tiers). |
| 43 | + Tier: privatecapb.CaPool_ENTERPRISE, |
| 44 | + } |
| 45 | + |
| 46 | + locationPath := fmt.Sprintf("projects/%s/locations/%s", projectID, location) |
| 47 | + |
| 48 | + // See https://pkg.go.dev/cloud.google.com/go/security/privateca/apiv1/privatecapb#CreateCaPoolRequest. |
| 49 | + req := &privatecapb.CreateCaPoolRequest{ |
| 50 | + Parent: locationPath, |
| 51 | + CaPoolId: caPoolId, |
| 52 | + CaPool: caPool, |
| 53 | + } |
| 54 | + |
| 55 | + op, err := caClient.CreateCaPool(ctx, req) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("CreateCaPool failed: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + if _, err = op.Wait(ctx); err != nil { |
| 61 | + return fmt.Errorf("CreateCaPool failed during wait: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Fprintf(w, "CA Pool created") |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// [END privateca_create_ca_pool] |
0 commit comments