|
| 1 | +/* |
| 2 | +Copyright © 2023 VMware, Inc. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MPL-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package inspectionsclient |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | + |
| 13 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/client/transport" |
| 14 | + "github.com/vmware/terraform-provider-tanzu-mission-control/internal/helper" |
| 15 | + inspectionsmodel "github.com/vmware/terraform-provider-tanzu-mission-control/internal/models/inspections" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // URL Paths. |
| 20 | + clustersAPIVersionAndGroupPath = "v1alpha1/clusters" |
| 21 | + inspectionsPath = "inspection/scans" |
| 22 | + |
| 23 | + // Query Params. |
| 24 | + managementClusterNameListInspectionsParam = "searchScope.managementClusterName" |
| 25 | + provisionerNameListInspectionsParam = "searchScope.provisionerName" |
| 26 | + managementClusterNameGetInspectionParam = "fullName.managementClusterName" |
| 27 | + provisionerNameGetInspectionParam = "fullName.provisionerName" |
| 28 | +) |
| 29 | + |
| 30 | +// New creates a new inspections resource service API client. |
| 31 | +func New(transport *transport.Client) ClientService { |
| 32 | + return &Client{Client: transport} |
| 33 | +} |
| 34 | + |
| 35 | +/* |
| 36 | +Client for inspections resource service API. |
| 37 | +*/ |
| 38 | +type Client struct { |
| 39 | + *transport.Client |
| 40 | +} |
| 41 | + |
| 42 | +// ClientService is the interface for Client methods. |
| 43 | +type ClientService interface { |
| 44 | + InspectionsResourceServiceList(fn *inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanFullName) (*inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanListData, error) |
| 45 | + |
| 46 | + InspectionsResourceServiceGet(fn *inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanFullName) (*inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanData, error) |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | +InspectionsResourceServiceList lists inspections. |
| 51 | +*/ |
| 52 | +func (c *Client) InspectionsResourceServiceList(fn *inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanFullName) (*inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanListData, error) { |
| 53 | + resp := &inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanListData{} |
| 54 | + |
| 55 | + if fn.ManagementClusterName == "" || fn.ProvisionerName == "" || fn.ClusterName == "" { |
| 56 | + return nil, errors.New("Management Cluster Name, Provisioner Name and Cluster Name must be provided.") |
| 57 | + } |
| 58 | + |
| 59 | + requestURL := helper.ConstructRequestURL(clustersAPIVersionAndGroupPath, fn.ClusterName, inspectionsPath) |
| 60 | + queryParams := url.Values{} |
| 61 | + |
| 62 | + queryParams.Add(managementClusterNameListInspectionsParam, fn.ManagementClusterName) |
| 63 | + queryParams.Add(provisionerNameListInspectionsParam, fn.ProvisionerName) |
| 64 | + |
| 65 | + requestURL = requestURL.AppendQueryParams(queryParams) |
| 66 | + |
| 67 | + err := c.Get(requestURL.String(), resp) |
| 68 | + |
| 69 | + return resp, err |
| 70 | +} |
| 71 | + |
| 72 | +/* |
| 73 | +InspectionsResourceServiceGet returns an inspection. |
| 74 | +*/ |
| 75 | +func (c *Client) InspectionsResourceServiceGet(fn *inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanFullName) (*inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanData, error) { |
| 76 | + resp := &inspectionsmodel.VmwareTanzuManageV1alpha1ClusterInspectionScanData{} |
| 77 | + |
| 78 | + if fn.ManagementClusterName == "" || fn.ProvisionerName == "" || fn.ClusterName == "" || fn.Name == "" { |
| 79 | + return nil, errors.New("Management Cluster Name, Provisioner Name, Cluster Name and Inspection Name must be provided.") |
| 80 | + } |
| 81 | + |
| 82 | + requestURL := helper.ConstructRequestURL(clustersAPIVersionAndGroupPath, fn.ClusterName, inspectionsPath, fn.Name) |
| 83 | + queryParams := url.Values{} |
| 84 | + |
| 85 | + queryParams.Add(managementClusterNameGetInspectionParam, fn.ManagementClusterName) |
| 86 | + queryParams.Add(provisionerNameGetInspectionParam, fn.ProvisionerName) |
| 87 | + |
| 88 | + requestURL = requestURL.AppendQueryParams(queryParams) |
| 89 | + |
| 90 | + err := c.Get(requestURL.String(), resp) |
| 91 | + |
| 92 | + return resp, err |
| 93 | +} |
0 commit comments