github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/workspace/v2/desktops/requests.go (about)

     1  package desktops
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/common/tags"
     6  	"github.com/chnsz/golangsdk/pagination"
     7  )
     8  
     9  // CreateOpts is the structure required by the Create method to create a new desktop.
    10  type CreateOpts struct {
    11  	// Configuration of system volume.
    12  	RootVolume *Volume `json:"root_volume" required:"true"`
    13  	// Configuration of desktops.
    14  	Desktops []DesktopConfig `json:"desktops" required:"true"`
    15  	// Desktop type.
    16  	// + DEDICATED: dedicated desktop.
    17  	DesktopType string `json:"desktop_type" required:"true"`
    18  	// Product ID of desktop.
    19  	ProductId string `json:"product_id" required:"true"`
    20  	// The availability zone where the desktop is located.
    21  	AvailabilityZone string `json:"availability_zone,omitempty"`
    22  	// Image type, the default value is private.
    23  	// + market
    24  	// + gold
    25  	// + private
    26  	ImageType string `json:"image_type,omitempty"`
    27  	// Image ID.
    28  	ImageId string `json:"image_id,omitempty"`
    29  	// Vpc ID, first creation time must be specified.
    30  	VpcId string `json:"vpc_id,omitempty"`
    31  	// Whether to send emails to user mailbox during important operations.
    32  	EmailNotification *bool `json:"email_notification,omitempty"`
    33  	// Configuration of data volumes.
    34  	DataVolumes []Volume `json:"data_volumes,omitempty"`
    35  	// NIC information corresponding to the desktop.
    36  	Nics []Nic `json:"nics,omitempty"`
    37  	// Configuration of security groups, the default security group (WorkspaceUserSecurityGroup) must be specified.
    38  	SecurityGroups []SecurityGroup `json:"security_groups,omitempty"`
    39  	// Specifies the key/value pairs of the desktop.
    40  	Tags []tags.ResourceTag `json:"tags,omitempty"`
    41  	// EnterpriseProject ID of desktop
    42  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    43  }
    44  
    45  // DesktopConfig is an object to specified the basic configuration of desktop.
    46  type DesktopConfig struct {
    47  	// User name.
    48  	UserName string `json:"user_name" required:"true"`
    49  	// User email.
    50  	UserEmail string `json:"user_email" required:"true"`
    51  	// User group.
    52  	UserGroup string `json:"user_group,omitempty"`
    53  	// Desktop name.
    54  	DesktopName string `json:"computer_name,omitempty"`
    55  	// Name prefix of desktop.
    56  	DesktopNamePrefix string `json:"desktop_name_prefix"`
    57  }
    58  
    59  // Volume is an object to specified the disk configuration of root volume or data volume.
    60  type Volume struct {
    61  	// Volume type.
    62  	// + **SAS**: High I/O disk type.
    63  	// + **SSD**: Ultra-high I/O disk type.
    64  	Type string `json:"type" required:"true"`
    65  	// Volume size.
    66  	// For root volume, the valid value is range from 80 to 1020.
    67  	// For data volume, the valid value is range from 10 to 8200.
    68  	Size int `json:"size" required:"true"`
    69  }
    70  
    71  // Nic is an object to specified the NIC information corresponding to the desktop.
    72  type Nic struct {
    73  	// Network ID.
    74  	NetworkId string `json:"subnet_id" required:"true"`
    75  }
    76  
    77  // SecurityGroup is an object to specified the security group to which the desktop belongs.
    78  type SecurityGroup struct {
    79  	ID string `json:"id" required:"true"`
    80  }
    81  
    82  var requestOpts = golangsdk.RequestOpts{
    83  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    84  }
    85  
    86  // Create is a method to create a desktop using given parameters.
    87  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*CreateResp, error) {
    88  	b, err := golangsdk.BuildRequestBody(opts, "")
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	var r CreateResp
    94  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
    95  		MoreHeaders: requestOpts.MoreHeaders,
    96  	})
    97  	return &r, err
    98  }
    99  
   100  // Get is a method to obtain the desktop detail by its ID.
   101  func Get(c *golangsdk.ServiceClient, desktopId string) (*Desktop, error) {
   102  	var r GetResp
   103  	_, err := c.Get(resourceURL(c, desktopId), &r, &golangsdk.RequestOpts{
   104  		MoreHeaders: requestOpts.MoreHeaders,
   105  	})
   106  	return &r.Desktop, err
   107  }
   108  
   109  // ProductUpdateOpts is the structure required by the UpdateProduct method to change the desktop product.
   110  type ProductUpdateOpts struct {
   111  	// Batch create configuration of desktop list.
   112  	Desktops []DesktopUpdateConfig `json:"desktops" required:"true"`
   113  	// Product ID.
   114  	ProductId string `json:"product_id" required:"true"`
   115  	// Whether the product ID can be changed when the desktop is powered on.
   116  	Mode string `json:"mode" required:"true"`
   117  }
   118  
   119  // DesktopUpdateConfig is an object to specified the update configuration of desktop.
   120  type DesktopUpdateConfig struct {
   121  	// Desktop ID.
   122  	DesktopId string `json:"desktop_id"`
   123  }
   124  
   125  // UpdateProduct is a method to create a desktop using given parameters.
   126  func UpdateProduct(c *golangsdk.ServiceClient, opts ProductUpdateOpts) ([]Job, error) {
   127  	b, err := golangsdk.BuildRequestBody(opts, "")
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	var r UpdateResp
   133  	_, err = c.Post(productURL(c), b, &r, &golangsdk.RequestOpts{
   134  		MoreHeaders: requestOpts.MoreHeaders,
   135  	})
   136  	return r.Jobs, err
   137  }
   138  
   139  // DeleteOpts is the structure required by the Delete method to delete an existing desktop.
   140  type DeleteOpts struct {
   141  	// Whether to delete user associated with this desktop after deleting it.
   142  	DeleteUser bool `q:"delete_users"`
   143  	// Whether to send emails to user mailbox during delete operation.
   144  	EmailNotification bool `q:"email_notification"`
   145  }
   146  
   147  // Delete is a method to remove an existing desktop using given parameters, if the user does not have any desktop under
   148  // it, the user can delete it together with the desktop.
   149  func Delete(c *golangsdk.ServiceClient, desktopId string, opts DeleteOpts) error {
   150  	url := resourceURL(c, desktopId)
   151  	query, err := golangsdk.BuildQueryString(opts)
   152  	if err != nil {
   153  		return err
   154  	}
   155  	url += query.String()
   156  
   157  	_, err = c.Delete(url, &golangsdk.RequestOpts{
   158  		MoreHeaders: requestOpts.MoreHeaders,
   159  	})
   160  	return err
   161  }
   162  
   163  // NewVolumeOpts is the structure required by the NewVolumes method to add some volumes to the desktop.
   164  type NewVolumeOpts struct {
   165  	// New volumes parameters.
   166  	VolumeConfigs []NewVolumeConfig `json:"addDesktopVolumesReq,omitempty"`
   167  }
   168  
   169  // NewVolumeConfig is an object to specified the volume configuration.
   170  type NewVolumeConfig struct {
   171  	// The desktop ID to which the volume belongs.
   172  	DesktopId string `json:"desktop_id,omitempty"`
   173  	//Configuration of data volumes.
   174  	Volumes []Volume `json:"volumes,omitempty"`
   175  }
   176  
   177  // NewVolumes is a method to add some new volumes to the desktop.
   178  func NewVolumes(c *golangsdk.ServiceClient, opts NewVolumeOpts) (*NewVolumesResp, error) {
   179  	b, err := golangsdk.BuildRequestBody(opts, "")
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	var r NewVolumesResp
   185  	_, err = c.Post(volumeURL(c), b, &r, &golangsdk.RequestOpts{
   186  		MoreHeaders: requestOpts.MoreHeaders,
   187  	})
   188  	return &r, err
   189  }
   190  
   191  // VolumeExpandOpts is the structure required by the ExpandVolumes method to batch expand volumes size.
   192  type VolumeExpandOpts struct {
   193  	// Volumes expansion parameters.
   194  	VolumeConfigs []ExpandVolumeConfig `json:"expandVolumesReq,omitempty"`
   195  }
   196  
   197  // ExpandVolumeConfig is an object to specified the volume configuration.
   198  type ExpandVolumeConfig struct {
   199  	// The desktop ID to which the volume belongs.
   200  	DesktopId string `json:"desktop_id,omitempty"`
   201  	// Volume ID.
   202  	VolumeId string `json:"volume_id,omitempty"`
   203  	// The size of the disk after resizing, in GB.
   204  	// For root volume, the valid value is range from 80 to 1020.
   205  	// For data volume, the valid value is range from 10 to 8200.
   206  	NewSize int `json:"new_size,omitempty"`
   207  }
   208  
   209  // ExpandVolumes is a method to batch expand the desktop volumes size.
   210  func ExpandVolumes(c *golangsdk.ServiceClient, opts VolumeExpandOpts) (*ExpandVolumesResp, error) {
   211  	b, err := golangsdk.BuildRequestBody(opts, "")
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  
   216  	var r ExpandVolumesResp
   217  	_, err = c.Post(volumeExpandURL(c), b, &r, &golangsdk.RequestOpts{
   218  		MoreHeaders: requestOpts.MoreHeaders,
   219  	})
   220  	return &r, err
   221  }
   222  
   223  // RebuildOpts is the structure that used to modify desktop image and os.
   224  type RebuildOpts struct {
   225  	// ID list of workspace desktops that wants to rebuild.
   226  	DesktopIds []string `json:"desktop_ids" required:"true"`
   227  	// New image type.
   228  	ImageType string `json:"image_type" required:"true"`
   229  	// New image ID.
   230  	ImageId string `json:"image_id" required:"true"`
   231  	// New OS type.
   232  	OsType string `json:"os_type,omitempty"`
   233  	// Delay time.
   234  	DelayTime string `json:"delay_time,omitempty"`
   235  	// Rebuild message send to the users.
   236  	Message string `json:"message,omitempty"`
   237  	// Enterprise project ID.
   238  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
   239  }
   240  
   241  // Rebuild is the method that used to modify desktop using given parameters.
   242  func Rebuild(c *golangsdk.ServiceClient, opts RebuildOpts) (*RebuildResp, error) {
   243  	b, err := golangsdk.BuildRequestBody(opts, "")
   244  	if err != nil {
   245  		return nil, err
   246  	}
   247  
   248  	var r RebuildResp
   249  	_, err = c.Post(rebuildURL(c), b, &r, &golangsdk.RequestOpts{
   250  		MoreHeaders: requestOpts.MoreHeaders,
   251  	})
   252  	return &r, err
   253  }
   254  
   255  // ListEipOpts is the structure that used to query the bound desktop and unbound EIPs.
   256  type ListEipOpts struct {
   257  	// EnterpriseProject ID of desktop.
   258  	EnterpriseProjectId string `q:"enterprise_project_id"`
   259  	// Desktop ID.
   260  	DesktopId string `q:"desktop_id"`
   261  	// Desktop name.
   262  	DesktopName string `q:"desktop_name"`
   263  	// User name.
   264  	UserName string `q:"user_name"`
   265  	// EIP address.
   266  	Address string `q:"address"`
   267  	// Offset from which the query starts.
   268  	// The starting record sequence number of the query starts from 0.
   269  	Offset int `q:"offset"`
   270  	// Number of items displayed on each page.
   271  	// If not specified, all matching records are returned.
   272  	Limit int `q:"limit"`
   273  	// EIP binding status.
   274  	// + bind: binded EIP
   275  	// + unbind: unbinded EIP
   276  	State string `q:"state"`
   277  }
   278  
   279  // ListEips is the method that used to query the EIPs in which bound desktop and unbound desktop.
   280  func ListEips(c *golangsdk.ServiceClient, desktopId string) ([]EipResp, error) {
   281  	url := eipsURL(c)
   282  	opts := ListEipOpts{
   283  		DesktopId: desktopId,
   284  	}
   285  	query, err := golangsdk.BuildQueryString(opts)
   286  	if err != nil {
   287  		return nil, err
   288  	}
   289  	url += query.String()
   290  
   291  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   292  		p := EipPage{pagination.OffsetPageBase{PageResult: r}}
   293  		return p
   294  	})
   295  	pager.Headers = requestOpts.MoreHeaders
   296  	pages, err := pager.AllPages()
   297  
   298  	if err != nil {
   299  		return nil, err
   300  	}
   301  
   302  	return ExtractEips(pages)
   303  }
   304  
   305  // BindEipOpts is the structure that used to bind EIP to desktop.
   306  type BindEipOpts struct {
   307  	// ID list of workspace desktops that wants to apply EIP.
   308  	DesktopId string `json:"desktop_id" required:"true"`
   309  	// EIP ID of worksoaces that wants to apply.
   310  	ID string `json:"eip_id" required:"true"`
   311  }
   312  
   313  // BindEip is the method that used to bind EIP to desktop.
   314  func BindEip(c *golangsdk.ServiceClient, opts BindEipOpts) error {
   315  	b, err := golangsdk.BuildRequestBody(opts, "")
   316  
   317  	if err != nil {
   318  		return err
   319  	}
   320  
   321  	_, err = c.Post(bindEipURL(c, "binding"), b, nil, &golangsdk.RequestOpts{
   322  		MoreHeaders: requestOpts.MoreHeaders,
   323  	})
   324  	return err
   325  }
   326  
   327  // UnbindEipOpt is the structure that used to unbind EIP from desktop.
   328  type UnbindEipOpt struct {
   329  	// Desktop ID of the EIP want to be unbind.
   330  	DesktopIds []string `json:"desktop_ids" required:"true"`
   331  }
   332  
   333  // UnbindEip is the method that used to unbind EIP from desktop.
   334  func UnbindEip(c *golangsdk.ServiceClient, opts UnbindEipOpt) error {
   335  	b, err := golangsdk.BuildRequestBody(opts, "")
   336  	if err != nil {
   337  		return err
   338  	}
   339  
   340  	_, err = c.Post(bindEipURL(c, "unbinding"), b, nil, &golangsdk.RequestOpts{
   341  		MoreHeaders: requestOpts.MoreHeaders,
   342  	})
   343  	return err
   344  }
   345  
   346  // GetNetwork is the method that used to query desktop network infomation.
   347  func GetNetwork(c *golangsdk.ServiceClient, desktopId string) ([]NetworkInfos, error) {
   348  	var r NetworkResp
   349  	_, err := c.Get(networkURL(c, desktopId), &r, &golangsdk.RequestOpts{
   350  		MoreHeaders: requestOpts.MoreHeaders,
   351  	})
   352  	return r.Network, err
   353  }
   354  
   355  // UpdateNetworkOpts is the structure that used to modify desktop network.
   356  type UpdateNetworkOpts struct {
   357  	DesktopId string `json:"-"`
   358  	// The ID of the vpc to be change.
   359  	VpcId string `json:"vpc_id" required:"true"`
   360  	// The ID of the subnet to be change.
   361  	SubnetId string `json:"subnet_id" required:"true"`
   362  	// ID list of security group.
   363  	SecurityGroupIds []string `json:"security_group_ids" required:"true"`
   364  	// Specifies a private ID address.
   365  	PrivateId string `json:"private_ip,omitempty"`
   366  }
   367  
   368  // UpdateNetwork is the method that used to modify desktop network infomation using given parameters.
   369  func UpdateNetwork(c *golangsdk.ServiceClient, opts UpdateNetworkOpts) (*UpdateNetworkResp, error) {
   370  	b, err := golangsdk.BuildRequestBody(opts, "")
   371  	if err != nil {
   372  		return nil, err
   373  	}
   374  
   375  	var r UpdateNetworkResp
   376  	_, err = c.Put(networkURL(c, opts.DesktopId), b, &r, &golangsdk.RequestOpts{
   377  		MoreHeaders: requestOpts.MoreHeaders,
   378  	})
   379  	return &r, err
   380  }
   381  
   382  // ActionOpts is the structure required by the DoAction method operate the power state of the desktop.
   383  type ActionOpts struct {
   384  	// ID list of workspace desktops that wants to operate.
   385  	DesktopIds []string `json:"desktop_ids" required:"true"`
   386  	// The power type of the desktop. The valid values are as follows:
   387  	// + os-start
   388  	// + os-stop
   389  	// + reboot
   390  	// + os-hibernate
   391  	OpType string `json:"op_type" requires:"true"`
   392  	// The operation type. The valid values are as follows:
   393  	// + SOFT: Normal operation.
   394  	// + HARD: Forced operation.
   395  	Type string `json:"type,omitempty"`
   396  }
   397  
   398  // DoAction is a method that used to operate the power state of the desktop.
   399  func DoAction(client *golangsdk.ServiceClient, opts ActionOpts) (*ActionResp, error) {
   400  	b, err := golangsdk.BuildRequestBody(opts, "")
   401  	if err != nil {
   402  		return nil, err
   403  	}
   404  
   405  	var r ActionResp
   406  	_, err = client.Post(actionURL(client), b, &r, &golangsdk.RequestOpts{
   407  		MoreHeaders: requestOpts.MoreHeaders,
   408  	})
   409  	return &r, err
   410  }