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

     1  package services
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/openstack/cse/dedicated/v4/auth"
     6  	"github.com/chnsz/golangsdk/openstack/cse/dedicated/v4/instances"
     7  )
     8  
     9  // CreateOpts is the structure required by the Create method to create a new dedicated microservice.
    10  type CreateOpts struct {
    11  	// Microservice information.
    12  	Services Service `json:"service" required:"true"`
    13  	// Blacklist and whitelist.
    14  	Rules []Rule `json:"rules,omitempty"`
    15  	// Instance information.
    16  	Instances []instances.CreateOpts `json:"instances,omitempty"`
    17  	// Extended attribute. You can customize a key and value. The value must be at least 1 byte long.
    18  	Tags map[string]interface{} `json:"tags,omitempty"`
    19  }
    20  
    21  // Service is an object that specifies the microservice configuration details.
    22  type Service struct {
    23  	// Microservice name, which must be unique in an application. The value contains 1 to 128 characters.
    24  	// Regular expression: ^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$
    25  	Name string `json:"serviceName" required:"true"`
    26  	// Application ID, which must be unique. The value contains 1 to 160 characters.
    27  	// Regular expression: ^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$
    28  	AppId string `json:"appId" required:"true"`
    29  	// Microservice version. The value contains 1 to 64 characters. Regular expression: ^[0-9]$|^[0-9]+(.[0-9]+)$
    30  	Version string `json:"version" required:"true"`
    31  	// Microservice ID, which must be unique. The value contains 1 to 64 characters. Regular expression: ^.*$
    32  	ID string `json:"serviceId,omitempty"`
    33  	// Service stage. Value: development, testing, acceptance, or production.
    34  	// Only when the value is development, testing, or acceptance, you can use the API for uploading schemas in batches
    35  	// to add or modify an existing schema. Default value: development.
    36  	Environment *string `json:"environment,omitempty"`
    37  	// Microservice description. The value contains a maximum of 256 characters.
    38  	Description string `json:"description,omitempty"`
    39  	// Microservice level. Value: FRONT, MIDDLE, or BACK.
    40  	Level string `json:"level,omitempty"`
    41  	// Microservice registration mode. Value: SDK, PLATFORM, SIDECAR, or UNKNOWN.
    42  	RegisterBy string `json:"registerBy,omitempty"`
    43  	// Foreign key ID of a microservice access schema. The array length supports a maximum of 100 schemas.
    44  	Schemas []string `json:"schemas,omitempty"`
    45  	// Microservice status. Value: UP or DOWN. Default value: UP.
    46  	Status string `json:"status,omitempty"`
    47  	// Microservice registration time.
    48  	Timestamp string `json:"timestamp,omitempty"`
    49  	// Latest modification time (UTC).
    50  	ModTimestamp string `json:"modTimestamp,omitempty"`
    51  	// Development framework.
    52  	Framework *Framework `json:"framework,omitempty"`
    53  	// Service path.
    54  	Paths []ServicePath `json:"paths,omitempty"`
    55  }
    56  
    57  // Framework is an object that specifies the configuration of the microservice framework.
    58  type Framework struct {
    59  	// Microservice development framework. Default value: UNKNOWN.
    60  	Name string `json:"name,omitempty"`
    61  	// Version of the microservice development framework.
    62  	Version string `json:"version,omitempty"`
    63  }
    64  
    65  // ServicePath is an object that specifies the configuration of the service path.
    66  type ServicePath struct {
    67  	// Route address.
    68  	Path string `json:"Path,omitempty"`
    69  	// Extended attribute. You can customize a key and value. The value must be at least 1 byte long.
    70  	Property map[string]interface{} `json:"Property,omitempty"`
    71  }
    72  
    73  // Rule is an object that specifies the configuration of the black list or white list.
    74  type Rule struct {
    75  	// Customized rule ID.
    76  	RuleId string `json:"ruleId,omitempty"`
    77  	// Rule type. Value: WHITE or BLACK.
    78  	RuleType string `json:"ruleType,omitempty"`
    79  	// If the value starts with tag_xxx, the attributes are filtered by Tag.
    80  	// Otherwise, the attributes are filtered by serviceId, AppId, ServiceName, Version, Description, Level, or Status.
    81  	Attribute string `json:"attribute,omitempty"`
    82  	// Matching rule. The value is a regular expression containing 1 to 64 characters.
    83  	Pattern string `json:"pattern,omitempty"`
    84  	// Rule description.
    85  	Description string `json:"description,omitempty"`
    86  	// Time when a rule is created. This parameter is used only when you query rules.
    87  	Timestamp string `json:"timestamp,omitempty"`
    88  	// Update time.
    89  	ModTimestamp string `json:"modTimestamp,omitempty"`
    90  }
    91  
    92  // Create is a method to create a microservice using given parameters.
    93  func Create(c *golangsdk.ServiceClient, opts CreateOpts, token string) (*CreateResp, error) {
    94  	b, err := golangsdk.BuildRequestBody(opts, "")
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	var r CreateResp
   100  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
   101  		JSONResponse: &r,
   102  		MoreHeaders:  auth.BuildMoreHeaderUsingToken(c, token),
   103  	})
   104  	return &r, err
   105  }
   106  
   107  // Get is a method to retrieves a particular configuration based on its unique ID (and token).
   108  func Get(c *golangsdk.ServiceClient, serviceId, token string) (*ServiceResp, error) {
   109  	var r struct {
   110  		Service ServiceResp `json:"service"`
   111  	}
   112  	_, err := c.Get(resourceURL(c, serviceId), &r, &golangsdk.RequestOpts{
   113  		MoreHeaders: auth.BuildMoreHeaderUsingToken(c, token),
   114  	})
   115  	return &r.Service, err
   116  }
   117  
   118  // DeleteOpts is the structure required by the Delete method to specified whether microservice is force delete.
   119  type DeleteOpts struct {
   120  	Force bool `q:"force"`
   121  }
   122  
   123  // Delete is a method to remove an existing microservice using its unique ID (and token).
   124  func Delete(c *golangsdk.ServiceClient, opts DeleteOpts, engineId, token string) error {
   125  	url := resourceURL(c, engineId)
   126  	query, err := golangsdk.BuildQueryString(opts)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	url += query.String()
   131  
   132  	_, err = c.Delete(url, &golangsdk.RequestOpts{
   133  		MoreHeaders: auth.BuildMoreHeaderUsingToken(c, token),
   134  	})
   135  	return err
   136  }