github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/softwaredeployment/requests.go (about)

     1  package softwaredeployment
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  // ListOpts allows the filtering and sorting of paginated collections through
    11  // the API.
    12  type ListOpts struct {
    13  	//Specifies the ID of the instance deployed by the software configuration.
    14  	ServerId string `q:"server_id"`
    15  	//Specifies the ID of this deployment resource.
    16  	Id string
    17  	//Specifies the ID of the software configuration resource running on an instance.
    18  	ConfigId string
    19  	//Specifies the current status of deployment resources. Valid values include COMPLETE, IN_PROGRESS, and FAILED.
    20  	Status string
    21  	// Specifies the stack action that triggers this deployment resource.
    22  	Action string
    23  }
    24  
    25  // List returns collection of
    26  // Software Deployment. It accepts a ListOpts struct, which allows you to filter and sort
    27  // the returned collection for greater efficiency.
    28  //
    29  // Default policy settings return only those Software Deployment that are owned by the
    30  // tenant who submits the request, unless an admin user submits the request.
    31  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Deployment, error) {
    32  	q, err := golangsdk.BuildQueryString(&opts)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	u := rootURL(client) + q.String()
    37  	pages, err := pagination.NewPager(client, u, func(r pagination.PageResult) pagination.Page {
    38  		return DeploymentPage{pagination.LinkedPageBase{PageResult: r}}
    39  	}).AllPages()
    40  
    41  	allConfigs, err := ExtractDeployments(pages)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return FilterDeployments(allConfigs, opts)
    47  }
    48  
    49  func FilterDeployments(deployments []Deployment, opts ListOpts) ([]Deployment, error) {
    50  
    51  	var refinedDeployments []Deployment
    52  	var matched bool
    53  	m := map[string]interface{}{}
    54  
    55  	if opts.Id != "" {
    56  		m["Id"] = opts.Id
    57  	}
    58  	if opts.ServerId != "" {
    59  		m["ServerId"] = opts.ServerId
    60  	}
    61  	if opts.ConfigId != "" {
    62  		m["ConfigId"] = opts.ConfigId
    63  	}
    64  	if opts.Status != "" {
    65  		m["Status"] = opts.Status
    66  	}
    67  	if opts.Action != "" {
    68  		m["Action"] = opts.Action
    69  	}
    70  
    71  	if len(m) > 0 && len(deployments) > 0 {
    72  		for _, deployment := range deployments {
    73  			matched = true
    74  
    75  			for key, value := range m {
    76  				if sVal := getStructField(&deployment, key); !(sVal == value) {
    77  					matched = false
    78  				}
    79  			}
    80  
    81  			if matched {
    82  				refinedDeployments = append(refinedDeployments, deployment)
    83  			}
    84  		}
    85  
    86  	} else {
    87  		refinedDeployments = deployments
    88  	}
    89  
    90  	return refinedDeployments, nil
    91  }
    92  
    93  func getStructField(v *Deployment, field string) string {
    94  	r := reflect.ValueOf(v)
    95  	f := reflect.Indirect(r).FieldByName(field)
    96  	return string(f.String())
    97  }
    98  
    99  // CreateOptsBuilder allows extensions to add additional parameters to the
   100  // Create request.
   101  type CreateOptsBuilder interface {
   102  	ToSoftwareDeploymentCreateMap() (map[string]interface{}, error)
   103  }
   104  
   105  // CreateOpts contains all the values needed to create a new Software Deployment. There are
   106  // no required values.
   107  type CreateOpts struct {
   108  	// Specifies the stack action that triggers this deployment resource.
   109  	Action string `json:"action,omitempty"`
   110  	//Specifies the ID of the software configuration resource running on an instance.
   111  	ConfigId string `json:"config_id" required:"true"`
   112  	//Specifies input data stored in the form of a key-value pair.
   113  	InputValues map[string]interface{} `json:"input_values,omitempty"`
   114  	//Specifies the ID of the instance deployed by the software configuration.
   115  	ServerId string `json:"server_id" required:"true"`
   116  	//Specifies the ID of the authenticated tenant who can perform operations on the deployment resources.
   117  	TenantId string `json:"stack_user_project_id,omitempty"`
   118  	//Specifies the current status of deployment resources. Valid values include COMPLETE, IN_PROGRESS, and FAILED.
   119  	Status string `json:"status,omitempty"`
   120  	//Specifies the cause of the current deployment resource status.
   121  	StatusReason string `json:"status_reason,omitempty"`
   122  }
   123  
   124  // ToSoftwareDeploymentCreateMap builds a create request body from CreateOpts.
   125  func (opts CreateOpts) ToSoftwareDeploymentCreateMap() (map[string]interface{}, error) {
   126  	return golangsdk.BuildRequestBody(opts, "")
   127  }
   128  
   129  // Create accepts a CreateOpts struct and uses the values to create a new Software Deployment
   130  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   131  	b, err := opts.ToSoftwareDeploymentCreateMap()
   132  	if err != nil {
   133  		r.Err = err
   134  		return
   135  	}
   136  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   137  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
   138  	return
   139  }
   140  
   141  // Get retrieves a particular software Deployment based on its unique ID.
   142  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   143  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   144  	return
   145  }
   146  
   147  //UpdateOptsBuilder is an interface by which can be able to build the request body of software deployment.
   148  type UpdateOptsBuilder interface {
   149  	ToSoftwareDeploymentUpdateMap() (map[string]interface{}, error)
   150  }
   151  
   152  //UpdateOpts is a struct which represents the request body of update method.
   153  type UpdateOpts struct {
   154  	// Specifies the stack action that triggers this deployment resource.
   155  	Action string `json:"action,omitempty"`
   156  	//Specifies the ID of the software configuration resource running on an instance.
   157  	ConfigId string `json:"config_id" required:"true"`
   158  	//Specifies input data stored in the form of a key-value pair.
   159  	InputValues map[string]interface{} `json:"input_values,omitempty"`
   160  	//Specifies output data stored in the form of a key-value pair.
   161  	OutputValues map[string]interface{} `json:"output_values" required:"true"`
   162  	//Specifies the current status of deployment resources. Valid values include COMPLETE, IN_PROGRESS, and FAILED.
   163  	Status string `json:"status,omitempty"`
   164  	//Specifies the cause of the current deployment resource status.
   165  	StatusReason string `json:"status_reason,omitempty"`
   166  }
   167  
   168  //ToSoftwareDeploymentUpdateMap builds a update request body from UpdateOpts.
   169  func (opts UpdateOpts) ToSoftwareDeploymentUpdateMap() (map[string]interface{}, error) {
   170  	return golangsdk.BuildRequestBody(opts, "")
   171  }
   172  
   173  //Update is a method which can be able to update the name of software deployment.
   174  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   175  	b, err := opts.ToSoftwareDeploymentUpdateMap()
   176  	if err != nil {
   177  		r.Err = err
   178  		return
   179  	}
   180  	_, r.Err = client.Put(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   181  		OkCodes: []int{200},
   182  	})
   183  	return
   184  }
   185  
   186  // Delete will permanently delete a particular Software Deployment based on its unique ID.
   187  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   188  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   189  	return
   190  }