github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/ecs/v1/cloudservers/results.go (about)

     1  package cloudservers
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	"github.com/chnsz/golangsdk"
     8  	"github.com/chnsz/golangsdk/pagination"
     9  )
    10  
    11  type cloudServerResult struct {
    12  	golangsdk.Result
    13  }
    14  
    15  type PasswordResult struct {
    16  	golangsdk.ErrResult
    17  }
    18  
    19  type UpdateResult struct {
    20  	golangsdk.ErrResult
    21  }
    22  type Flavor struct {
    23  	Disk  string `json:"disk"`
    24  	Vcpus string `json:"vcpus"`
    25  	RAM   string `json:"ram"`
    26  	ID    string `json:"id"`
    27  	Name  string `json:"name"`
    28  }
    29  
    30  // Image defines a image struct in details of a server.
    31  type Image struct {
    32  	ID string `json:"id"`
    33  }
    34  
    35  type SysTags struct {
    36  	Key   string `json:"key"`
    37  	Value string `json:"value"`
    38  }
    39  
    40  type OsSchedulerHints struct {
    41  	Group           []string `json:"group"`
    42  	Tenancy         []string `json:"tenancy"`
    43  	DedicatedHostID []string `json:"dedicated_host_id"`
    44  	FaultDomain     string   `json:"fault_domain,omitempty"`
    45  }
    46  
    47  // Metadata is only used for method that requests details on a single server, by ID.
    48  // Because metadata struct must be a map.
    49  type Metadata struct {
    50  	ChargingMode      string `json:"charging_mode"`
    51  	OrderID           string `json:"metering.order_id"`
    52  	ProductID         string `json:"metering.product_id"`
    53  	VpcID             string `json:"vpc_id"`
    54  	EcmResStatus      string `json:"EcmResStatus"`
    55  	ImageID           string `json:"metering.image_id"`
    56  	Imagetype         string `json:"metering.imagetype"`
    57  	Resourcespeccode  string `json:"metering.resourcespeccode"`
    58  	ImageName         string `json:"image_name"`
    59  	OsBit             string `json:"os_bit"`
    60  	LockCheckEndpoint string `json:"lock_check_endpoint"`
    61  	LockSource        string `json:"lock_source"`
    62  	LockSourceID      string `json:"lock_source_id"`
    63  	LockScene         string `json:"lock_scene"`
    64  	VirtualEnvType    string `json:"virtual_env_type"`
    65  	AgencyName        string `json:"agency_name"`
    66  	AgentList         string `json:"__support_agent_list"`
    67  }
    68  
    69  type Address struct {
    70  	Version string `json:"version"`
    71  	Addr    string `json:"addr"`
    72  	MacAddr string `json:"OS-EXT-IPS-MAC:mac_addr"`
    73  	PortID  string `json:"OS-EXT-IPS:port_id"`
    74  	Type    string `json:"OS-EXT-IPS:type"`
    75  }
    76  
    77  type VolumeAttached struct {
    78  	ID                  string `json:"id"`
    79  	DeleteOnTermination string `json:"delete_on_termination"`
    80  	BootIndex           string `json:"bootIndex"`
    81  	Device              string `json:"device"`
    82  }
    83  
    84  type SecurityGroups struct {
    85  	Name string `json:"name"`
    86  	ID   string `json:"id"`
    87  }
    88  
    89  // CloudServer is only used for method that requests details on a single server, by ID.
    90  // Because metadata struct must be a map.
    91  type CloudServer struct {
    92  	Status              string               `json:"status"`
    93  	Updated             time.Time            `json:"updated"`
    94  	HostID              string               `json:"hostId"`
    95  	Addresses           map[string][]Address `json:"addresses"`
    96  	ID                  string               `json:"id"`
    97  	Name                string               `json:"name"`
    98  	AccessIPv4          string               `json:"accessIPv4"`
    99  	AccessIPv6          string               `json:"accessIPv6"`
   100  	Created             time.Time            `json:"created"`
   101  	Tags                []string             `json:"tags"`
   102  	Description         string               `json:"description"`
   103  	Locked              *bool                `json:"locked"`
   104  	ConfigDrive         string               `json:"config_drive"`
   105  	TenantID            string               `json:"tenant_id"`
   106  	UserID              string               `json:"user_id"`
   107  	HostStatus          string               `json:"host_status"`
   108  	EnterpriseProjectID string               `json:"enterprise_project_id"`
   109  	SysTags             []SysTags            `json:"sys_tags"`
   110  	Flavor              Flavor               `json:"flavor"`
   111  	Metadata            Metadata             `json:"metadata"`
   112  	SecurityGroups      []SecurityGroups     `json:"security_groups"`
   113  	KeyName             string               `json:"key_name"`
   114  	Image               Image                `json:"image"`
   115  	Progress            *int                 `json:"progress"`
   116  	PowerState          *int                 `json:"OS-EXT-STS:power_state"`
   117  	VMState             string               `json:"OS-EXT-STS:vm_state"`
   118  	TaskState           string               `json:"OS-EXT-STS:task_state"`
   119  	DiskConfig          string               `json:"OS-DCF:diskConfig"`
   120  	AvailabilityZone    string               `json:"OS-EXT-AZ:availability_zone"`
   121  	LaunchedAt          string               `json:"OS-SRV-USG:launched_at"`
   122  	TerminatedAt        string               `json:"OS-SRV-USG:terminated_at"`
   123  	RootDeviceName      string               `json:"OS-EXT-SRV-ATTR:root_device_name"`
   124  	RamdiskID           string               `json:"OS-EXT-SRV-ATTR:ramdisk_id"`
   125  	KernelID            string               `json:"OS-EXT-SRV-ATTR:kernel_id"`
   126  	LaunchIndex         *int                 `json:"OS-EXT-SRV-ATTR:launch_index"`
   127  	ReservationID       string               `json:"OS-EXT-SRV-ATTR:reservation_id"`
   128  	Hostname            string               `json:"OS-EXT-SRV-ATTR:hostname"`
   129  	UserData            string               `json:"OS-EXT-SRV-ATTR:user_data"`
   130  	Host                string               `json:"OS-EXT-SRV-ATTR:host"`
   131  	InstanceName        string               `json:"OS-EXT-SRV-ATTR:instance_name"`
   132  	HypervisorHostname  string               `json:"OS-EXT-SRV-ATTR:hypervisor_hostname"`
   133  	VolumeAttached      []VolumeAttached     `json:"os-extended-volumes:volumes_attached"`
   134  	OsSchedulerHints    OsSchedulerHints     `json:"os:scheduler_hints"`
   135  	Fault               Fault                `json:"fault"`
   136  	AutoTerminateTime   string               `json:"auto_terminate_time"`
   137  }
   138  
   139  // ECS fault causes
   140  type Fault struct {
   141  	Code    int       `json:"code"`
   142  	Created time.Time `json:"created"`
   143  	Details string    `json:"details"`
   144  	Message string    `json:"message"`
   145  }
   146  
   147  // NewCloudServer defines the response from details on a single server, by ID.
   148  type NewCloudServer struct {
   149  	CloudServer
   150  	Metadata map[string]string `json:"metadata"`
   151  }
   152  
   153  // GetResult is the response from a Get operation. Call its Extract
   154  // method to interpret it as a Server.
   155  type GetResult struct {
   156  	cloudServerResult
   157  }
   158  
   159  func (r GetResult) Extract() (*CloudServer, error) {
   160  	var s struct {
   161  		Server *CloudServer `json:"server"`
   162  	}
   163  	err := r.ExtractInto(&s)
   164  	return s.Server, err
   165  }
   166  
   167  // ServerPage abstracts the raw results of making a List() request against
   168  // the API.
   169  type ServerPage struct {
   170  	pagination.PageSizeBase
   171  }
   172  
   173  // IsEmpty returns true if a page contains no Server results.
   174  func (r ServerPage) IsEmpty() (bool, error) {
   175  	s, err := ExtractServers(r)
   176  	return len(s) == 0, err
   177  }
   178  
   179  // NextPageURL returns the next page with offset and limit.
   180  func (r ServerPage) NextPageURL() (string, error) {
   181  	pageName := "offset"
   182  	currentURL := r.URL
   183  
   184  	q := currentURL.Query()
   185  	pageNum := q.Get(pageName)
   186  	if pageNum == "" {
   187  		pageNum = "1"
   188  	}
   189  
   190  	sizeVal, err := strconv.ParseInt(pageNum, 10, 32)
   191  	if err != nil {
   192  		return "", err
   193  	}
   194  
   195  	pageNum = strconv.Itoa(int(sizeVal + 1))
   196  	q.Set(pageName, pageNum)
   197  	currentURL.RawQuery = q.Encode()
   198  	return currentURL.String(), nil
   199  }
   200  
   201  // ExtractServers interprets the results of a single page from a List() call,
   202  // producing a slice of CloudServer entities.
   203  func ExtractServers(r pagination.Page) ([]CloudServer, error) {
   204  	var s struct {
   205  		Servers []CloudServer `json:"servers"`
   206  	}
   207  
   208  	err := (r.(ServerPage)).ExtractInto(&s)
   209  	return s.Servers, err
   210  }
   211  
   212  // UpdateMetadataResult contains the result of an UpdateMetadata operation.
   213  // Call its Extract method to interpret it as a map[string]interface{}.
   214  type UpdateMetadataResult struct {
   215  	golangsdk.Result
   216  }
   217  
   218  // DeleteMetadatItemResult contains the result of a DeleteMetadatItem operation.
   219  // Call its ExtractErr method to determine if the call succeeded or failed.
   220  type DeleteMetadatItemResult struct {
   221  	golangsdk.ErrResult
   222  }
   223  
   224  func (r UpdateMetadataResult) Extract() (map[string]interface{}, error) {
   225  	var s struct {
   226  		Metadata map[string]interface{} `json:"metadata"`
   227  	}
   228  	err := r.ExtractInto(&s)
   229  	return s.Metadata, err
   230  }