Skip to content

Commit

Permalink
Add update method. Tests passing
Browse files Browse the repository at this point in the history
Signed-off-by: abarreiro <abarreiro@vmware.com>
  • Loading branch information
adambarreiro committed May 23, 2023
1 parent 243de5f commit e3952d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .changes/v2.21.0/575-features.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* Added methods to create, get, publish and delete UI Plugins `VCDClient.AddUIPlugin`, `VCDClient.GetAllUIPlugins`,
`VCDClient.GetUIPluginById`, `VCDClient.GetUIPlugin`, `UIPlugin.Enable`, `UIPlugin.Disable`, `UIPlugin.GetPublishedTenants`,
`VCDClient.GetUIPluginById`, `VCDClient.GetUIPlugin`, `UIPlugin.Update`, `UIPlugin.GetPublishedTenants`,
`UIPlugin.PublishAll`, `UIPlugin.UnublishAll`, `UIPlugin.Publish`, `UIPlugin.Unublish`, `UIPlugin.IsTheSameAs` and `UIPlugin.Delete` [GH-575]
80 changes: 35 additions & 45 deletions govcd/ui_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ func (vcdClient *VCDClient) GetUIPlugin(vendor, pluginName, version string) (*UI
return nil, fmt.Errorf("could not find any UI plugin with vendor '%s', pluginName '%s' and version '%s': %s", vendor, pluginName, version, ErrorEntityNotFound)
}

// Enable enables the receiver UI plugin.
func (uiPlugin *UIPlugin) Enable() error {
return uiPlugin.enableOrDisable(true)
}

// Disable disables the receiver UI plugin.
func (uiPlugin *UIPlugin) Disable() error {
return uiPlugin.enableOrDisable(false)
}

func (uiPlugin *UIPlugin) GetPublishedTenants() (types.OpenApiReferences, error) {
if strings.TrimSpace(uiPlugin.UIPluginMetadata.ID) == "" {
return nil, fmt.Errorf("plugin ID is required but it is empty")
Expand Down Expand Up @@ -196,6 +186,41 @@ func (uiPlugin *UIPlugin) IsTheSameAs(otherUiPlugin *UIPlugin) bool {
uiPlugin.UIPluginMetadata.Vendor == otherUiPlugin.UIPluginMetadata.Vendor
}

// Update performs an update to several receiver plugin attributes
func (uiPlugin *UIPlugin) Update(enable, providerScoped, tenantScoped bool) error {
if strings.TrimSpace(uiPlugin.UIPluginMetadata.ID) == "" {
return fmt.Errorf("plugin ID is required but it is 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
}

payload := &types.UIPluginMetadata{
Vendor: uiPlugin.UIPluginMetadata.Vendor,
License: uiPlugin.UIPluginMetadata.License,
Link: uiPlugin.UIPluginMetadata.Link,
PluginName: uiPlugin.UIPluginMetadata.PluginName,
Version: uiPlugin.UIPluginMetadata.Version,
Description: uiPlugin.UIPluginMetadata.Description,
ProviderScoped: providerScoped,
TenantScoped: tenantScoped,
Enabled: enable,
}
err = uiPlugin.client.OpenApiPutItem(apiVersion, urlRef, nil, payload, uiPlugin.UIPluginMetadata, nil)
if err != nil {
return err
}
return nil
}

// Delete deletes the receiver UIPlugin.
func (uiPlugin *UIPlugin) Delete() error {
if strings.TrimSpace(uiPlugin.UIPluginMetadata.ID) == "" {
Expand Down Expand Up @@ -394,38 +419,3 @@ func publishOrUnpublishFromOrgs(client *Client, pluginId string, orgs types.Open

return client.OpenApiPostItem(apiVersion, urlRef, nil, orgs, nil, nil)
}

// enableOrDisable enables or disables the receiver UI plugin, depending on the input.
func (uiPlugin *UIPlugin) enableOrDisable(enable bool) error {
if strings.TrimSpace(uiPlugin.UIPluginMetadata.ID) == "" {
return fmt.Errorf("plugin ID is required but it is 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
}

payload := &types.UIPluginMetadata{
Vendor: uiPlugin.UIPluginMetadata.Vendor,
License: uiPlugin.UIPluginMetadata.License,
Link: uiPlugin.UIPluginMetadata.Link,
PluginName: uiPlugin.UIPluginMetadata.PluginName,
Version: uiPlugin.UIPluginMetadata.Version,
Description: uiPlugin.UIPluginMetadata.Description,
ProviderScoped: uiPlugin.UIPluginMetadata.ProviderScoped,
TenantScoped: uiPlugin.UIPluginMetadata.TenantScoped,
Enabled: enable,
}
err = uiPlugin.client.OpenApiPutItem(apiVersion, urlRef, nil, payload, uiPlugin.UIPluginMetadata, nil)
if err != nil {
return err
}
return nil
}
8 changes: 6 additions & 2 deletions govcd/ui_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,18 @@ func (vcd *TestVCD) Test_UIPlugin(check *C) {
}

// Check that the plugin can be disabled
err = newUIPlugin.Disable()
err = newUIPlugin.Update(false, false, false)
check.Assert(err, IsNil)
check.Assert(newUIPlugin.UIPluginMetadata.Enabled, Equals, false)
check.Assert(newUIPlugin.UIPluginMetadata.ProviderScoped, Equals, false)
check.Assert(newUIPlugin.UIPluginMetadata.TenantScoped, Equals, false)

// Check that the plugin can be enabled again
err = newUIPlugin.Enable()
err = newUIPlugin.Update(true, true, true)
check.Assert(err, IsNil)
check.Assert(newUIPlugin.UIPluginMetadata.Enabled, Equals, true)
check.Assert(newUIPlugin.UIPluginMetadata.ProviderScoped, Equals, true)
check.Assert(newUIPlugin.UIPluginMetadata.TenantScoped, Equals, true)

// Delete the created plugin
err = newUIPlugin.Delete()
Expand Down

0 comments on commit e3952d9

Please sign in to comment.