From 0ac72981db92fc042569695f04603820a22a8b09 Mon Sep 17 00:00:00 2001 From: abarreiro Date: Mon, 22 May 2023 17:29:15 +0200 Subject: [PATCH] Add and Delete. Tests pass Signed-off-by: abarreiro --- govcd/common_test.go | 2 +- govcd/openapi_endpoints.go | 15 +++++++------- govcd/ui_plugin.go | 40 +++++++++++++++++++++++++------------- govcd/ui_plugin_test.go | 5 ++++- types/v56/constants.go | 3 +-- types/v56/openapi.go | 3 ++- 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/govcd/common_test.go b/govcd/common_test.go index f7fc56602..fb32bc451 100644 --- a/govcd/common_test.go +++ b/govcd/common_test.go @@ -1,4 +1,4 @@ -//go:build api || auth || functional || catalog || vapp || gateway || network || org || query || extnetwork || task || vm || vdc || system || disk || lb || lbAppRule || lbAppProfile || lbServerPool || lbServiceMonitor || lbVirtualServer || user || role || nsxv || nsxt || openapi || affinity || search || alb || certificate || vdcGroup || metadata || providervdc || rde || ALL +//go:build api || auth || functional || catalog || vapp || gateway || network || org || query || extnetwork || task || vm || vdc || system || disk || lb || lbAppRule || lbAppProfile || lbServerPool || lbServiceMonitor || lbVirtualServer || user || role || nsxv || nsxt || openapi || affinity || search || alb || certificate || vdcGroup || metadata || providervdc || rde || plugin || ALL /* * Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License. diff --git a/govcd/openapi_endpoints.go b/govcd/openapi_endpoints.go index b236b26a5..ff81c518f 100644 --- a/govcd/openapi_endpoints.go +++ b/govcd/openapi_endpoints.go @@ -64,13 +64,6 @@ var endpointMinApiVersions = map[string]string{ types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntities: "35.0", // VCD 10.2+ types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntitiesTypes: "35.0", // VCD 10.2+ types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointRdeEntitiesResolve: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUi: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUiPlugin: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUiPublishAll: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUiPublish: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUiUnpublishAll: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUiUnpublish: "35.0", // VCD 10.2+ - types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointTransfer: "35.0", // VCD 10.2+ // NSX-T ALB (Advanced/AVI Load Balancer) support was introduced in 10.2 types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbController: "35.0", // VCD 10.2+ @@ -96,6 +89,14 @@ var endpointMinApiVersions = map[string]string{ types.OpenApiPathVersion2_0_0 + types.OpenApiEndpointVdcAssignedComputePolicies: "35.0", types.OpenApiPathVersion2_0_0 + types.OpenApiEndpointVdcComputePolicies: "35.0", types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcNetworkProfile: "36.0", // VCD 10.3+ + + // Extensions API endpoints. These are not versioned + types.OpenApiEndpointExtensionsUi: "35.0", // VCD 10.2+ + types.OpenApiEndpointExtensionsUiPlugin: "35.0", // VCD 10.2+ + types.OpenApiEndpointExtensionsUiPublishAll: "35.0", // VCD 10.2+ + types.OpenApiEndpointExtensionsUiPublish: "35.0", // VCD 10.2+ + types.OpenApiEndpointExtensionsUiUnpublishAll: "35.0", // VCD 10.2+ + types.OpenApiEndpointExtensionsUiUnpublish: "35.0", // VCD 10.2+ } // endpointElevatedApiVersions endpoint elevated API versions diff --git a/govcd/ui_plugin.go b/govcd/ui_plugin.go index c87304d3e..7bf868a70 100644 --- a/govcd/ui_plugin.go +++ b/govcd/ui_plugin.go @@ -40,6 +40,25 @@ func (vcdClient *VCDClient) AddUIPlugin(pluginPath string) (*UIPlugin, error) { return uiPluginMetadata, nil } +func (uiPlugin *UIPlugin) Delete() error { + if strings.TrimSpace(uiPlugin.UIPluginMetadata.ID) == "" { + return fmt.Errorf("plugin ID must not be empty") + } + + endpoint := types.OpenApiEndpointExtensionsUi // This one is not versioned, hence not using types.OpenApiPathVersion1_0_0 or alike + apiVersion, err := uiPlugin.client.getOpenApiHighestElevatedVersion(endpoint) + if err != nil { + return err + } + + urlRef, err := uiPlugin.client.OpenApiBuildEndpoint(endpoint, uiPlugin.UIPluginMetadata.ID) + if err != nil { + return err + } + + return uiPlugin.client.OpenApiDeleteItem(apiVersion, urlRef, nil, nil) +} + // getPluginMetadata retrieves the UI Plugin Metadata information stored inside the given .zip file. func getPluginMetadata(pluginPath string) (*types.UIPluginMetadata, error) { archive, err := zip.OpenReader(filepath.Clean(pluginPath)) @@ -97,7 +116,7 @@ func getPluginMetadata(pluginPath string) (*types.UIPluginMetadata, error) { // createUIPlugin creates a new UI extension and sets the provided plugin metadata for it. // Only System administrator can create a UI extension. func createUIPlugin(client *Client, uiPluginMetadata *types.UIPluginMetadata) (*UIPlugin, error) { - endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUi + endpoint := types.OpenApiEndpointExtensionsUi // This one is not versioned, hence not using types.OpenApiPathVersion1_0_0 or alike apiVersion, err := client.getOpenApiHighestElevatedVersion(endpoint) if err != nil { return nil, err @@ -128,13 +147,13 @@ func (ui *UIPlugin) upload(pluginPath string) error { return err } - endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointExtensionsUiPlugin + endpoint := types.OpenApiEndpointExtensionsUiPlugin // This one is not versioned, hence not using types.OpenApiPathVersion1_0_0 or alike apiVersion, err := ui.client.getOpenApiHighestElevatedVersion(endpoint) if err != nil { return err } - urlRef, err := ui.client.OpenApiBuildEndpoint(endpoint) + urlRef, err := ui.client.OpenApiBuildEndpoint(fmt.Sprintf(endpoint, ui.UIPluginMetadata.ID)) if err != nil { return err } @@ -143,7 +162,7 @@ func (ui *UIPlugin) upload(pluginPath string) error { FileName: filepath.Base(pluginPath), ChecksumAlgo: "sha256", Checksum: fmt.Sprintf("%x", sha256.Sum256(fileContents)), - Size: len(fileContents), + Size: int64(len(fileContents)), } headers, err := ui.client.OpenApiPostItemAndGetHeaders(apiVersion, urlRef, nil, uploadSpec, nil, nil) @@ -156,18 +175,17 @@ func (ui *UIPlugin) upload(pluginPath string) error { return err } - endpoint = types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointTransfer - apiVersion, err = ui.client.getOpenApiHighestElevatedVersion(endpoint) + transferEndpoint := fmt.Sprintf("%s://%s/transfer/%s", ui.client.VCDHREF.Scheme, ui.client.VCDHREF.Host, transferId) + request, err := newFileUploadRequest(ui.client, transferEndpoint, fileContents, 0, uploadSpec.Size, uploadSpec.Size) if err != nil { return err } - urlRef, err = ui.client.OpenApiBuildEndpoint(endpoint, transferId) + response, err := ui.client.Http.Do(request) if err != nil { return err } - - return ui.client.OpenApiPutItem(apiVersion, urlRef, nil, fileContents, nil, nil) + return response.Body.Close() } // getTransferIdFromHeader retrieves a valid transfer ID from any given HTTP headers @@ -199,7 +217,3 @@ func (*UIPlugin) Unpublish(orgs types.OpenApiReferences) (types.OpenApiReference func (*UIPlugin) UnpublishAll() { } - -func (*UIPlugin) Delete() { - -} diff --git a/govcd/ui_plugin_test.go b/govcd/ui_plugin_test.go index a6efb8662..accfe3b52 100644 --- a/govcd/ui_plugin_test.go +++ b/govcd/ui_plugin_test.go @@ -17,7 +17,10 @@ func init() { } func (vcd *TestVCD) Test_UIPlugin(check *C) { - _, err := vcd.client.AddUIPlugin("") + uiPlugin, err := vcd.client.AddUIPlugin("../test-resources/ui_plugin.zip") + check.Assert(err, IsNil) + + err = uiPlugin.Delete() check.Assert(err, IsNil) } diff --git a/types/v56/constants.go b/types/v56/constants.go index 802b65e33..7e2c92cfe 100644 --- a/types/v56/constants.go +++ b/types/v56/constants.go @@ -399,13 +399,12 @@ const ( OpenApiEndpointRdeEntities = "entities/" OpenApiEndpointRdeEntitiesTypes = "entities/types/" OpenApiEndpointRdeEntitiesResolve = "entities/%s/resolve" - OpenApiEndpointExtensionsUi = "extensions/ui" + OpenApiEndpointExtensionsUi = "extensions/ui/" OpenApiEndpointExtensionsUiPlugin = "extensions/ui/%s/plugin" OpenApiEndpointExtensionsUiPublishAll = "extensions/ui/%s/tenants/publishAll" OpenApiEndpointExtensionsUiPublish = "extensions/ui/%s/tenants/publish" OpenApiEndpointExtensionsUiUnpublishAll = "extensions/ui/%s/tenants/unpublishAll" OpenApiEndpointExtensionsUiUnpublish = "extensions/ui/%s/tenants/unpublish" - OpenApiEndpointTransfer = "transfer/" // NSX-T ALB related endpoints diff --git a/types/v56/openapi.go b/types/v56/openapi.go index 17983fb9f..308a6d851 100644 --- a/types/v56/openapi.go +++ b/types/v56/openapi.go @@ -468,6 +468,7 @@ type DefinedEntity struct { // UIPluginMetadata gives meta information about a UI Plugin type UIPluginMetadata struct { + ID string `json:"id,omitempty"` Vendor string `json:"vendor,omitempty"` License string `json:"license,omitempty"` Link string `json:"link,omitempty"` @@ -482,7 +483,7 @@ type UIPluginMetadata struct { // UploadSpec gives information about an upload type UploadSpec struct { FileName string `json:"fileName,omitempty"` - Size int `json:"size,omitempty"` + Size int64 `json:"size,omitempty"` Checksum string `json:"checksum,omitempty"` ChecksumAlgo string `json:"checksumAlgo,omitempty"` }