github.com/oam-dev/kubevela@v1.9.11/references/cli/provider.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cli
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/gosuri/uitable"
    23  	terraformapi "github.com/oam-dev/terraform-controller/api/v1beta1"
    24  	"github.com/spf13/cobra"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	"github.com/oam-dev/kubevela/apis/types"
    28  	"github.com/oam-dev/kubevela/pkg/utils/common"
    29  	cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
    30  )
    31  
    32  // NewProviderCommand create `addon` command
    33  // +Deprecated
    34  func NewProviderCommand(c common.Args, order string, ioStreams cmdutil.IOStreams) *cobra.Command {
    35  	cmd := &cobra.Command{
    36  		Use:   "provider",
    37  		Short: "Authenticate terraform cloud providers.",
    38  		Long:  "Authenticate terraform cloud providers by managing terraform controller providers with its credential secret.",
    39  		Annotations: map[string]string{
    40  			types.TagCommandOrder: order,
    41  			types.TagCommandType:  types.TypeLegacy,
    42  		},
    43  	}
    44  	cmd.AddCommand(
    45  		NewProviderListCommand(c, ioStreams),
    46  	)
    47  	cmd.AddCommand(prepareProviderAddCommand())
    48  	cmd.AddCommand(prepareProviderDeleteCommand())
    49  	return cmd
    50  }
    51  
    52  // NewProviderListCommand create addon list command
    53  func NewProviderListCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
    54  	return &cobra.Command{
    55  		Use:     "list",
    56  		Aliases: []string{"ls"},
    57  		Short:   "List Terraform Cloud Providers",
    58  		Long:    "List Terraform Cloud Providers",
    59  		RunE: func(cmd *cobra.Command, args []string) error {
    60  			k8sClient, err := c.GetClient()
    61  			if err != nil {
    62  				return err
    63  			}
    64  			err = listProviders(context.Background(), k8sClient, ioStreams)
    65  			if err != nil {
    66  				return err
    67  			}
    68  			return nil
    69  		},
    70  	}
    71  }
    72  
    73  func prepareProviderAddCommand() *cobra.Command {
    74  	cmd := &cobra.Command{
    75  		Use:        "add",
    76  		Deprecated: "Please use the vela config command: \n vela config create <name> --template <provider-type> [Properties]",
    77  	}
    78  	return cmd
    79  }
    80  
    81  func listProviders(ctx context.Context, k8sClient client.Client, ioStreams cmdutil.IOStreams) error {
    82  	l := &terraformapi.ProviderList{}
    83  	if err := k8sClient.List(ctx, l, client.InNamespace(types.ProviderNamespace)); err != nil {
    84  		return err
    85  	}
    86  
    87  	table := uitable.New()
    88  	table.AddRow("TYPE", "PROVIDER", "NAME", "REGION", "CREATED-TIME")
    89  
    90  	for _, p := range l.Items {
    91  		table.AddRow(p.Labels["config.oam.dev/provider"], p.Spec.Provider, p.Name, p.Spec.Region, p.CreationTimestamp)
    92  	}
    93  	ioStreams.Info(table.String())
    94  	return nil
    95  }
    96  
    97  func prepareProviderDeleteCommand() *cobra.Command {
    98  	cmd := &cobra.Command{
    99  		Use:        "delete",
   100  		Aliases:    []string{"rm", "del"},
   101  		Deprecated: "Please use the vela config command: \n  vela config delete <provider-name>",
   102  	}
   103  	return cmd
   104  }