github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/csbs/v1/resource/GetResBackupCapabilities.go (about)

     1  package resource
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
     6  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/csbs/v1/backup"
     8  )
     9  
    10  // ResourceBackupCapOpts contains the options for querying whether resources can be backed up.
    11  type ResourceBackupCapOpts struct {
    12  	// ID of the resource (server, or EVS disk) to be checked
    13  	// For details about how to obtain the server ID, see the Elastic Cloud Server API Reference.
    14  	// For details about how to obtain the disk ID, see the Elastic Volume Service API Reference.
    15  	ResourceId string `json:"resource_id" required:"true"`
    16  	// Type of the resource to be checked, for example, OS::Nova::Server for an ECS
    17  	ResourceType string `json:"resource_type" required:"true"`
    18  }
    19  
    20  // GetResBackupCapabilities will query whether resources can be backed up based on the values in ResourceBackupCapOpts. To extract
    21  // the ResourceCap object from the response, call the ExtractQueryResponse method on the QueryResult.
    22  func GetResBackupCapabilities(client *golangsdk.ServiceClient, opts []ResourceBackupCapOpts) ([]ResourceCapability, error) {
    23  	return doAction(client, opts, "check_protectable", "protectable")
    24  }
    25  
    26  func doAction(client *golangsdk.ServiceClient, opts interface{}, parent, label string) ([]ResourceCapability, error) {
    27  	b, err := build.RequestBody(opts, parent)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	// POST https://{endpoint}/v1/{project_id}/providers/{provider_id}/resources/action
    33  	raw, err := client.Post(client.ServiceURL("providers", backup.ProviderID, "resources", "action"), b, nil, &golangsdk.RequestOpts{
    34  		OkCodes: []int{200},
    35  	})
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	var res []ResourceCapability
    41  	err = extract.IntoSlicePtr(raw.Body, &res, label)
    42  	return res, err
    43  }
    44  
    45  type ResourceCapability struct {
    46  	// Whether backup or restoration is supported
    47  	// true: yes
    48  	// false: no
    49  	Result bool `json:"result"`
    50  	// Resource type
    51  	// Possible values are OS::Nova::Server (ECS) and OS::Ironic::BareMetalServer (BMS).
    52  	ResourceType string `json:"resource_type"`
    53  	// Error code. If an error occurs, a value is returned.
    54  	ErrorCode string `json:"error_code"`
    55  	// Error message, which will be returned if the VM is associated with a backup policy. If an error occurs, a value is returned.
    56  	ErrorMsg string `json:"error_msg"`
    57  	// Resource ID
    58  	ResourceId string `json:"resource_id"`
    59  }