github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/cce/v3/clusters/requests.go (about)

     1  package clusters
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  )
     8  
     9  var 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 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  type ExpirationOptsBuilder interface {
   130  	ToExpirationGetMap() (map[string]interface{}, error)
   131  }
   132  
   133  type ExpirationOpts struct {
   134  	Duration int `json:"duration" required:"true"`
   135  }
   136  
   137  func (opts ExpirationOpts) ToExpirationGetMap() (map[string]interface{}, error) {
   138  	return golangsdk.BuildRequestBody(opts, "")
   139  }
   140  
   141  // Create accepts a CreateOpts struct and uses the values to create a new
   142  // logical cluster.
   143  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   144  	b, err := opts.ToClusterCreateMap()
   145  	if err != nil {
   146  		r.Err = err
   147  		return
   148  	}
   149  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
   150  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
   151  	return
   152  }
   153  
   154  // Get retrieves a particular cluster based on its unique ID.
   155  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   156  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{
   157  		OkCodes:     []int{200},
   158  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   159  	})
   160  	return
   161  }
   162  
   163  // GetCert retrieves a particular cluster certificate based on its unique ID.
   164  func GetCert(c *golangsdk.ServiceClient, id string) (r GetCertResult) {
   165  	_, r.Err = c.Get(certificateURL(c, id), &r.Body, &golangsdk.RequestOpts{
   166  		OkCodes:     []int{200},
   167  		MoreHeaders: RequestOpts.MoreHeaders,
   168  	})
   169  	return
   170  }
   171  
   172  // GetCertWithExpiration retrieves a particular cluster certificate based on its unique ID.
   173  func GetCertWithExpiration(c *golangsdk.ServiceClient, id string, opts ExpirationOptsBuilder) (r GetCertResult) {
   174  	b, err := opts.ToExpirationGetMap()
   175  	if err != nil {
   176  		r.Err = err
   177  		return
   178  	}
   179  
   180  	_, r.Err = c.Post(certificateURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   181  		OkCodes:     []int{200},
   182  		MoreHeaders: RequestOpts.MoreHeaders,
   183  	})
   184  	return
   185  }
   186  
   187  // UpdateOpts contains all the values needed to update a new cluster
   188  type UpdateOpts struct {
   189  	Spec UpdateSpec `json:"spec" required:"true"`
   190  }
   191  
   192  type UpdateSpec struct {
   193  	// Cluster description
   194  	Description string `json:"description,omitempty"`
   195  }
   196  
   197  // UpdateOptsBuilder allows extensions to add additional parameters to the
   198  // Update request.
   199  type UpdateOptsBuilder interface {
   200  	ToClusterUpdateMap() (map[string]interface{}, error)
   201  }
   202  
   203  // ToClusterUpdateMap builds an update body based on UpdateOpts.
   204  func (opts UpdateOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
   205  	return golangsdk.BuildRequestBody(opts, "")
   206  }
   207  
   208  // Update allows clusters to update description.
   209  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   210  	b, err := opts.ToClusterUpdateMap()
   211  	if err != nil {
   212  		r.Err = err
   213  		return
   214  	}
   215  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   216  		OkCodes: []int{200},
   217  	})
   218  	return
   219  }
   220  
   221  // Delete will permanently delete a particular cluster based on its unique ID.
   222  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   223  	_, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{
   224  		OkCodes:     []int{200},
   225  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   226  	})
   227  	return
   228  }
   229  
   230  type DeleteOpts struct {
   231  	ErrorStatus string `q:"errorStatus"`
   232  	DeleteEfs   string `q:"delete_efs"`
   233  	DeleteENI   string `q:"delete_eni"`
   234  	DeleteEvs   string `q:"delete_evs"`
   235  	DeleteNet   string `q:"delete_net"`
   236  	DeleteObs   string `q:"delete_obs"`
   237  	DeleteSfs   string `q:"delete_sfs"`
   238  }
   239  
   240  func DeleteWithOpts(c *golangsdk.ServiceClient, id string, opts DeleteOpts) error {
   241  	url := resourceURL(c, id)
   242  	q, err := golangsdk.BuildQueryString(&opts)
   243  	if err != nil {
   244  		return err
   245  	}
   246  
   247  	_, err = c.Delete(url+q.String(), &golangsdk.RequestOpts{
   248  		OkCodes:     []int{200},
   249  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   250  	})
   251  	return err
   252  }
   253  
   254  type UpdateIpOpts struct {
   255  	Action    string `json:"action" required:"true"`
   256  	Spec      IpSpec `json:"spec,omitempty"`
   257  	ElasticIp string `json:"elasticIp"`
   258  }
   259  
   260  type IpSpec struct {
   261  	ID string `json:"id" required:"true"`
   262  }
   263  
   264  type UpdateIpOptsBuilder interface {
   265  	ToMasterIpUpdateMap() (map[string]interface{}, error)
   266  }
   267  
   268  func (opts UpdateIpOpts) ToMasterIpUpdateMap() (map[string]interface{}, error) {
   269  	return golangsdk.BuildRequestBody(opts, "spec")
   270  }
   271  
   272  // Update the access information of a specified cluster.
   273  func UpdateMasterIp(c *golangsdk.ServiceClient, id string, opts UpdateIpOptsBuilder) (r UpdateIpResult) {
   274  	b, err := opts.ToMasterIpUpdateMap()
   275  	if err != nil {
   276  		r.Err = err
   277  		return
   278  	}
   279  	_, r.Err = c.Put(masterIpURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   280  		OkCodes: []int{200},
   281  	})
   282  	return
   283  }