|
| 1 | +// Copyright 2024 Confluent Inc. All Rights Reserved. |
| 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 | +// http://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 v2 |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/spf13/cobra" |
| 22 | + |
| 23 | + piv2 "github.com/confluentinc/ccloud-sdk-go-v2/provider-integration/v2" |
| 24 | + |
| 25 | + pcmd "github.com/confluentinc/cli/v4/pkg/cmd" |
| 26 | + "github.com/confluentinc/cli/v4/pkg/examples" |
| 27 | +) |
| 28 | + |
| 29 | +func (c *command) newCreateCommand() *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "create <display-name>", |
| 32 | + Short: "Create a provider integration.", |
| 33 | + Long: "Create a provider integration that allows users to manage access to cloud provider resources through Confluent resources. The integration is created in DRAFT state.", |
| 34 | + Args: cobra.ExactArgs(1), |
| 35 | + RunE: c.create, |
| 36 | + Example: examples.BuildExampleString( |
| 37 | + examples.Example{ |
| 38 | + Text: `Create Azure provider integration "azure-integration" in the current environment.`, |
| 39 | + Code: "confluent provider-integration v2 create azure-integration --cloud azure", |
| 40 | + }, |
| 41 | + examples.Example{ |
| 42 | + Text: `Create GCP provider integration "gcp-integration" in environment "env-123456".`, |
| 43 | + Code: "confluent provider-integration v2 create gcp-integration --cloud gcp --environment env-123456", |
| 44 | + }, |
| 45 | + ), |
| 46 | + } |
| 47 | + |
| 48 | + cmd.Flags().String("cloud", "", fmt.Sprintf("Cloud provider (%s or %s).", providerAzure, providerGcp)) |
| 49 | + pcmd.AddEnvironmentFlag(cmd, c.AuthenticatedCLICommand) |
| 50 | + pcmd.AddContextFlag(cmd, c.CLICommand) |
| 51 | + pcmd.AddOutputFlag(cmd) |
| 52 | + |
| 53 | + cobra.CheckErr(cmd.MarkFlagRequired("cloud")) |
| 54 | + |
| 55 | + return cmd |
| 56 | +} |
| 57 | + |
| 58 | +func (c *command) create(cmd *cobra.Command, args []string) error { |
| 59 | + displayName := args[0] |
| 60 | + |
| 61 | + cloud, err := cmd.Flags().GetString("cloud") |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + cloud = strings.ToLower(cloud) |
| 67 | + if cloud != providerAzure && cloud != providerGcp { |
| 68 | + return fmt.Errorf("cloud provider must be either %s or %s", providerAzure, providerGcp) |
| 69 | + } |
| 70 | + |
| 71 | + environmentId, err := c.Context.EnvironmentId() |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + // Create the integration in DRAFT state |
| 77 | + request := piv2.PimV2Integration{ |
| 78 | + DisplayName: &displayName, |
| 79 | + Provider: &cloud, |
| 80 | + Environment: &piv2.ObjectReference{Id: environmentId}, |
| 81 | + } |
| 82 | + |
| 83 | + providerIntegration, err := c.V2Client.CreatePimV2Integration(cmd.Context(), request) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + return printProviderIntegrationTable(cmd, providerIntegration) |
| 89 | +} |
0 commit comments