github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/remoteconsoles/requests.go (about)

     1  package remoteconsoles
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  )
     8  
     9  // ConsoleProtocol represents valid remote console protocol.
    10  // It can be used to create a remote console with one of the pre-defined protocol.
    11  type ConsoleProtocol string
    12  
    13  const (
    14  	// ConsoleProtocolVNC represents the VNC console protocol.
    15  	ConsoleProtocolVNC ConsoleProtocol = "vnc"
    16  
    17  	// ConsoleProtocolSPICE represents the SPICE console protocol.
    18  	ConsoleProtocolSPICE ConsoleProtocol = "spice"
    19  
    20  	// ConsoleProtocolRDP represents the RDP console protocol.
    21  	ConsoleProtocolRDP ConsoleProtocol = "rdp"
    22  
    23  	// ConsoleProtocolSerial represents the Serial console protocol.
    24  	ConsoleProtocolSerial ConsoleProtocol = "serial"
    25  
    26  	// ConsoleProtocolMKS represents the MKS console protocol.
    27  	ConsoleProtocolMKS ConsoleProtocol = "mks"
    28  )
    29  
    30  // ConsoleType represents valid remote console type.
    31  // It can be used to create a remote console with one of the pre-defined type.
    32  type ConsoleType string
    33  
    34  const (
    35  	// ConsoleTypeNoVNC represents the VNC console type.
    36  	ConsoleTypeNoVNC ConsoleType = "novnc"
    37  
    38  	// ConsoleTypeXVPVNC represents the XVP VNC console type.
    39  	ConsoleTypeXVPVNC ConsoleType = "xvpvnc"
    40  
    41  	// ConsoleTypeRDPHTML5 represents the RDP HTML5 console type.
    42  	ConsoleTypeRDPHTML5 ConsoleType = "rdp-html5"
    43  
    44  	// ConsoleTypeSPICEHTML5 represents the SPICE HTML5 console type.
    45  	ConsoleTypeSPICEHTML5 ConsoleType = "spice-html5"
    46  
    47  	// ConsoleTypeSerial represents the Serial console type.
    48  	ConsoleTypeSerial ConsoleType = "serial"
    49  
    50  	// ConsoleTypeWebMKS represents the Web MKS console type.
    51  	ConsoleTypeWebMKS ConsoleType = "webmks"
    52  )
    53  
    54  // CreateOptsBuilder allows to add additional parameters to the Create request.
    55  type CreateOptsBuilder interface {
    56  	ToRemoteConsoleCreateMap() (map[string]any, error)
    57  }
    58  
    59  // CreateOpts specifies parameters to the Create request.
    60  type CreateOpts struct {
    61  	// Protocol specifies the protocol of a new remote console.
    62  	Protocol ConsoleProtocol `json:"protocol" required:"true"`
    63  
    64  	// Type specifies the type of a new remote console.
    65  	Type ConsoleType `json:"type" required:"true"`
    66  }
    67  
    68  // ToRemoteConsoleCreateMap builds a request body from the CreateOpts.
    69  func (opts CreateOpts) ToRemoteConsoleCreateMap() (map[string]any, error) {
    70  	return gophercloud.BuildRequestBody(opts, "remote_console")
    71  }
    72  
    73  // Create requests the creation of a new remote console on the specified server.
    74  func Create(ctx context.Context, client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) {
    75  	reqBody, err := opts.ToRemoteConsoleCreateMap()
    76  	if err != nil {
    77  		r.Err = err
    78  		return
    79  	}
    80  
    81  	resp, err := client.Post(ctx, createURL(client, serverID), reqBody, &r.Body, &gophercloud.RequestOpts{
    82  		OkCodes: []int{200},
    83  	})
    84  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    85  	return
    86  }