Skip to content

Commit

Permalink
Add and Delete. Tests pass
Browse files Browse the repository at this point in the history
Signed-off-by: abarreiro <abarreiro@vmware.com>
  • Loading branch information
adambarreiro committed May 22, 2023
1 parent 72f91f5 commit 0ac7298
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion govcd/common_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 8 additions & 7 deletions govcd/openapi_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand All @@ -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
Expand Down
40 changes: 27 additions & 13 deletions govcd/ui_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -199,7 +217,3 @@ func (*UIPlugin) Unpublish(orgs types.OpenApiReferences) (types.OpenApiReference
func (*UIPlugin) UnpublishAll() {

}

func (*UIPlugin) Delete() {

}
5 changes: 4 additions & 1 deletion govcd/ui_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions types/v56/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion types/v56/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}

0 comments on commit 0ac7298

Please sign in to comment.