github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cse/dedicated/v4/instances/requests.go (about)

     1  package instances
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/cse/dedicated/v4/auth"
     6  )
     7  
     8  // CreateOpts is the structure required by the Create method to create a new dedicated microservice instance.
     9  type CreateOpts struct {
    10  	// Host information.
    11  	HostName string `json:"hostName" required:"true"`
    12  	// Access address information.
    13  	Endpoints []string `json:"endpoints" required:"true"`
    14  	// Microservice version.
    15  	Version string `json:"version,omitempty"`
    16  	// Instance status. Value: UP, DOWN, STARTING, or OUTOFSERVICE. Default value: UP.
    17  	Status string `json:"status,omitempty"`
    18  	// Extended attribute. You can customize a key and value. The value must be at least 1 byte long.
    19  	Properties map[string]interface{} `json:"properties,omitempty"`
    20  	// Health check information.
    21  	HealthCheck *HealthCheck `json:"healthCheck,omitempty"`
    22  	// Data center information.
    23  	DataCenterInfo *DataCenter `json:"dataCenterInfo,omitempty"`
    24  }
    25  
    26  // HealthCheck is an object that specifies the configuration of the instance health check.
    27  type HealthCheck struct {
    28  	// Heartbeat mode. Value: push or pull.
    29  	Mode string `json:"mode" required:"true"`
    30  	// Heartbeat interval. Unit: s. If the value is less than 5s, the registration is performed at an interval of 5s.
    31  	Interval int `json:"interval" required:"true"`
    32  	// Maximum retries.
    33  	Times int `json:"times" required:"true"`
    34  	// Port.
    35  	Port int `json:"port,omitempty"`
    36  }
    37  
    38  // DataCenter is an object that specifies the configuration of the instance data center.
    39  type DataCenter struct {
    40  	// Region name.
    41  	Name string `json:"name" required:"true"`
    42  	// Region.
    43  	Region string `json:"region" required:"true"`
    44  	// AZ.
    45  	AvailableZone string `json:"availableZone" required:"true"`
    46  }
    47  
    48  // Create is a method to create a dedicated microservice instance using given parameters.
    49  func Create(c *golangsdk.ServiceClient, opts CreateOpts, serviceId, token string) (*CreateResp, error) {
    50  	b, err := golangsdk.BuildRequestBody(opts, "instance")
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	var r CreateResp
    56  	_, err = c.Post(rootURL(c, serviceId), b, &r, &golangsdk.RequestOpts{
    57  		MoreHeaders: auth.BuildMoreHeaderUsingToken(c, token),
    58  	})
    59  	return &r, err
    60  }
    61  
    62  // Get is a method to retrieves a particular configuration based on its unique ID (and token).
    63  func Get(c *golangsdk.ServiceClient, serviceId, instanceId, token string) (*Instance, error) {
    64  	var r struct {
    65  		Instance Instance `json:"instance"`
    66  	}
    67  	_, err := c.Get(resourceURL(c, serviceId, instanceId), &r, &golangsdk.RequestOpts{
    68  		MoreHeaders: auth.BuildMoreHeaderUsingToken(c, token),
    69  	})
    70  	return &r.Instance, err
    71  }
    72  
    73  // List is a method to retrieves all instance informations under specified microservice using its related microservice
    74  // ID (and token).
    75  func List(c *golangsdk.ServiceClient, serviceId string, token string) ([]Instance, error) {
    76  	var r struct {
    77  		Instances []Instance `json:"instances"`
    78  	}
    79  	_, err := c.Get(rootURL(c, serviceId), &r, &golangsdk.RequestOpts{
    80  		MoreHeaders: auth.BuildMoreHeaderUsingToken(c, token),
    81  	})
    82  	return r.Instances, err
    83  }
    84  
    85  // Delete is a method to remove an existing instance using its related microservice ID, instance ID (and token).
    86  func Delete(c *golangsdk.ServiceClient, serviceId, instanceId, token string) error {
    87  	_, err := c.Delete(resourceURL(c, serviceId, instanceId), &golangsdk.RequestOpts{
    88  		MoreHeaders: auth.BuildMoreHeaderUsingToken(c, token),
    89  	})
    90  	return err
    91  }