github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cce/v3/clusters/requests.go (about)

     1  package clusters
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
    10  	MoreHeaders: map[string]string{"Content-Type": "application/json"},
    11  }
    12  
    13  // ListOpts allows the filtering of list data using given parameters.
    14  type ListOpts struct {
    15  	Name  string `json:"name"`
    16  	ID    string `json:"uuid"`
    17  	Type  string `json:"type"`
    18  	VpcID string `json:"vpc"`
    19  	Phase string `json:"phase"`
    20  }
    21  
    22  // List returns collection of clusters.
    23  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Clusters, error) {
    24  	var r ListResult
    25  	_, r.Err = client.Get(rootURL(client), &r.Body, &golangsdk.RequestOpts{
    26  		OkCodes:     []int{200},
    27  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
    28  	})
    29  
    30  	allClusters, err := r.ExtractClusters()
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	return FilterClusters(allClusters, opts), nil
    36  }
    37  
    38  func FilterClusters(clusters []Clusters, opts ListOpts) []Clusters {
    39  
    40  	var refinedClusters []Clusters
    41  	var matched bool
    42  	m := map[string]FilterStruct{}
    43  
    44  	if opts.Name != "" {
    45  		m["Name"] = FilterStruct{Value: opts.Name, Driller: []string{"Metadata"}}
    46  	}
    47  	if opts.ID != "" {
    48  		m["Id"] = FilterStruct{Value: opts.ID, Driller: []string{"Metadata"}}
    49  	}
    50  	if opts.Type != "" {
    51  		m["Type"] = FilterStruct{Value: opts.Type, Driller: []string{"Spec"}}
    52  	}
    53  	if opts.VpcID != "" {
    54  		m["VpcId"] = FilterStruct{Value: opts.VpcID, Driller: []string{"Spec", "HostNetwork"}}
    55  	}
    56  	if opts.Phase != "" {
    57  		m["Phase"] = FilterStruct{Value: opts.Phase, Driller: []string{"Status"}}
    58  	}
    59  
    60  	if len(m) > 0 && len(clusters) > 0 {
    61  		for _, cluster := range clusters {
    62  			matched = true
    63  
    64  			for key, value := range m {
    65  				if sVal := GetStructNestedField(&cluster, key, value.Driller); !(sVal == value.Value) {
    66  					matched = false
    67  				}
    68  			}
    69  			if matched {
    70  				refinedClusters = append(refinedClusters, cluster)
    71  			}
    72  		}
    73  
    74  	} else {
    75  		refinedClusters = clusters
    76  	}
    77  
    78  	return refinedClusters
    79  }
    80  
    81  type FilterStruct struct {
    82  	Value   string
    83  	Driller []string
    84  }
    85  
    86  func GetStructNestedField(v *Clusters, field string, structDriller []string) string {
    87  	r := reflect.ValueOf(v)
    88  	for _, drillField := range structDriller {
    89  		f := reflect.Indirect(r).FieldByName(drillField).Interface()
    90  		r = reflect.ValueOf(f)
    91  	}
    92  	f1 := reflect.Indirect(r).FieldByName(field)
    93  	return string(f1.String())
    94  }
    95  
    96  // CreateOptsBuilder allows extensions to add additional parameters to the
    97  // Create request.
    98  type CreateOptsBuilder interface {
    99  	ToClusterCreateMap() (map[string]interface{}, error)
   100  }
   101  
   102  // CreateOpts contains all the values needed to create a new cluster
   103  type CreateOpts struct {
   104  	// API type, fixed value Cluster
   105  	Kind string `json:"kind" required:"true"`
   106  	// API version, fixed value v3
   107  	ApiVersion string `json:"apiversion" required:"true"`
   108  	// Metadata required to create a cluster
   109  	Metadata CreateMetaData `json:"metadata" required:"true"`
   110  	// specifications to create a cluster
   111  	Spec Spec `json:"spec" required:"true"`
   112  }
   113  
   114  // Metadata required to create a cluster
   115  type CreateMetaData struct {
   116  	// Cluster unique name
   117  	Name string `json:"name" required:"true"`
   118  	// Cluster tag, key/value pair format
   119  	Labels map[string]string `json:"labels,omitempty"`
   120  	// Cluster annotation, key/value pair format
   121  	Annotations map[string]string `json:"annotations,omitempty"`
   122  }
   123  
   124  // ToClusterCreateMap builds a create request body from CreateOpts.
   125  func (opts CreateOpts) ToClusterCreateMap() (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
   130  // logical cluster.
   131  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   132  	b, err := opts.ToClusterCreateMap()
   133  	if err != nil {
   134  		r.Err = err
   135  		return
   136  	}
   137  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
   138  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
   139  	return
   140  }
   141  
   142  // Get retrieves a particular cluster based on its unique ID.
   143  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   144  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{
   145  		OkCodes:     []int{200},
   146  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   147  	})
   148  	return
   149  }
   150  
   151  // GetCert retrieves a particular cluster certificate based on its unique ID.
   152  func GetCert(c *golangsdk.ServiceClient, id string) (r GetCertResult) {
   153  	_, r.Err = c.Get(certificateURL(c, id), &r.Body, &golangsdk.RequestOpts{
   154  		OkCodes:     []int{200},
   155  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   156  	})
   157  	return
   158  }
   159  
   160  // UpdateOpts contains all the values needed to update a new cluster
   161  type UpdateOpts struct {
   162  	Spec UpdateSpec `json:"spec" required:"true"`
   163  }
   164  
   165  type UpdateSpec struct {
   166  	// Cluster description
   167  	Description string `json:"description,omitempty"`
   168  }
   169  
   170  // UpdateOptsBuilder allows extensions to add additional parameters to the
   171  // Update request.
   172  type UpdateOptsBuilder interface {
   173  	ToClusterUpdateMap() (map[string]interface{}, error)
   174  }
   175  
   176  // ToClusterUpdateMap builds an update body based on UpdateOpts.
   177  func (opts UpdateOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
   178  	return golangsdk.BuildRequestBody(opts, "")
   179  }
   180  
   181  // Update allows clusters to update description.
   182  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   183  	b, err := opts.ToClusterUpdateMap()
   184  	if err != nil {
   185  		r.Err = err
   186  		return
   187  	}
   188  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   189  		OkCodes: []int{200},
   190  	})
   191  	return
   192  }
   193  
   194  type DeleteOpts struct {
   195  	ErrorStatus string `q:"errorStatus"`
   196  	DeleteEfs   string `q:"delete_efs"`
   197  	DeleteENI   string `q:"delete_eni"`
   198  	DeleteEvs   string `q:"delete_evs"`
   199  	DeleteNet   string `q:"delete_net"`
   200  	DeleteObs   string `q:"delete_obs"`
   201  	DeleteSfs   string `q:"delete_sfs"`
   202  }
   203  
   204  type DeleteOptsBuilder interface {
   205  	ToDeleteQuery() (string, error)
   206  }
   207  
   208  func (opts DeleteOpts) ToDeleteQuery() (string, error) {
   209  	q, err := golangsdk.BuildQueryString(opts)
   210  	return q.String(), err
   211  }
   212  
   213  // DeleteWithOpts will permanently delete a particular cluster based on its unique ID,
   214  // and can delete associated resources based on DeleteOpts.
   215  func DeleteWithOpts(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
   216  	url := resourceURL(c, id)
   217  	if opts != nil {
   218  		var query string
   219  		query, r.Err = opts.ToDeleteQuery()
   220  		if r.Err != nil {
   221  			return
   222  		}
   223  		url += query
   224  	}
   225  	_, r.Err = c.Delete(url, &golangsdk.RequestOpts{
   226  		OkCodes:     []int{200},
   227  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   228  	})
   229  	return
   230  }
   231  
   232  // Delete will permanently delete a particular cluster based on its unique ID.
   233  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   234  	_, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{
   235  		OkCodes:     []int{200},
   236  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   237  	})
   238  	return
   239  }
   240  
   241  type UpdateIpOpts struct {
   242  	Action    string `json:"action" required:"true"`
   243  	Spec      IpSpec `json:"spec,omitempty"`
   244  	ElasticIp string `json:"elasticIp"`
   245  }
   246  
   247  type IpSpec struct {
   248  	ID string `json:"id" required:"true"`
   249  }
   250  
   251  type UpdateIpOptsBuilder interface {
   252  	ToMasterIpUpdateMap() (map[string]interface{}, error)
   253  }
   254  
   255  func (opts UpdateIpOpts) ToMasterIpUpdateMap() (map[string]interface{}, error) {
   256  	return golangsdk.BuildRequestBody(opts, "spec")
   257  }
   258  
   259  // Update the access information of a specified cluster.
   260  func UpdateMasterIp(c *golangsdk.ServiceClient, id string, opts UpdateIpOptsBuilder) (r UpdateIpResult) {
   261  	b, err := opts.ToMasterIpUpdateMap()
   262  	if err != nil {
   263  		r.Err = err
   264  		return
   265  	}
   266  	_, r.Err = c.Put(masterIpURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   267  		OkCodes: []int{200},
   268  	})
   269  	return
   270  }
   271  
   272  func Operation(c *golangsdk.ServiceClient, id, action string) (r OperationResult) {
   273  	_, r.Err = c.Post(operationURL(c, id, action), nil, nil, &golangsdk.RequestOpts{
   274  		OkCodes:     []int{200},
   275  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   276  	})
   277  	return
   278  }