github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/rts/v1/softwaredeployment/requests.go (about)

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