|
| 1 | +/* |
| 2 | +Copyright 2023 VMware, Inc. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MPL-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package kubeconfig |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/client/transport" |
| 12 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper" |
| 13 | + models "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/kubeconfig" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + apiVersionAndGroup = "/v1alpha1/clusters" |
| 18 | + apiKubeconfigPath = "kubeconfig" |
| 19 | + queryParamKeyFullNameManagementClusterName = "full_name.managementClusterName" |
| 20 | + queryParamKeyFullNameProvisionerName = "full_name.provisionerName" |
| 21 | +) |
| 22 | + |
| 23 | +// New creates a new kubeconfig service API client. |
| 24 | +func New(transport *transport.Client) ClientService { |
| 25 | + return &Client{Client: transport} |
| 26 | +} |
| 27 | + |
| 28 | +/* |
| 29 | +Client for kubeconfig service API. |
| 30 | +*/ |
| 31 | +type Client struct { |
| 32 | + *transport.Client |
| 33 | +} |
| 34 | + |
| 35 | +// ClientService is the interface for Client methods. |
| 36 | +type ClientService interface { |
| 37 | + KubeconfigServiceGet(fn *models.VmwareTanzuManageV1alpha1ClusterFullName) (*models.VmwareTanzuManageV1alpha1ClusterKubeconfigGetKubeconfigResponse, error) |
| 38 | +} |
| 39 | + |
| 40 | +/* |
| 41 | +KubeconfigServiceGet gets cluster kubeconfig. |
| 42 | +*/ |
| 43 | +func (c *Client) KubeconfigServiceGet(fn *models.VmwareTanzuManageV1alpha1ClusterFullName) (*models.VmwareTanzuManageV1alpha1ClusterKubeconfigGetKubeconfigResponse, error) { |
| 44 | + queryParams := url.Values{} |
| 45 | + if fn.ManagementClusterName != "" { |
| 46 | + queryParams.Add(queryParamKeyFullNameManagementClusterName, fn.ManagementClusterName) |
| 47 | + } |
| 48 | + |
| 49 | + if fn.ProvisionerName != "" { |
| 50 | + queryParams.Add(queryParamKeyFullNameProvisionerName, fn.ProvisionerName) |
| 51 | + } |
| 52 | + |
| 53 | + queryParams.Add("cli", string(models.VmwareTanzuManageV1alpha1ClusterKubeconfigCliTypeTANZUCLI)) |
| 54 | + |
| 55 | + requestURL := helper.ConstructRequestURL(apiVersionAndGroup, fn.Name, apiKubeconfigPath).AppendQueryParams(queryParams).String() |
| 56 | + clusterResponse := &models.VmwareTanzuManageV1alpha1ClusterKubeconfigGetKubeconfigResponse{} |
| 57 | + |
| 58 | + err := c.Get(requestURL, clusterResponse) |
| 59 | + |
| 60 | + return clusterResponse, err |
| 61 | +} |
0 commit comments