github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cts/v1/tracker/requests.go (about)

     1  package tracker
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  )
     8  
     9  // ListOpts allows the filtering and sorting of paginated collections through
    10  // the API. Filtering is achieved by passing in struct field values that map to
    11  // the attributes you want to see returned.
    12  type ListOpts struct {
    13  	TrackerName    string
    14  	BucketName     string
    15  	FilePrefixName string
    16  	Status         string
    17  }
    18  
    19  // List returns collection of Tracker. It accepts a ListOpts struct, which allows you to filter and sort
    20  // the returned collection for greater efficiency.
    21  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Tracker, error) {
    22  	var r ListResult
    23  	_, r.Err = client.Get(rootURL(client), &r.Body, &golangsdk.RequestOpts{
    24  		OkCodes: []int{200},
    25  	})
    26  
    27  	allTracker, err := r.ExtractTracker()
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	return FilterTracker(allTracker, opts)
    33  }
    34  
    35  func FilterTracker(tracker []Tracker, opts ListOpts) ([]Tracker, error) {
    36  
    37  	var refinedTracker []Tracker
    38  	var matched bool
    39  	m := map[string]interface{}{}
    40  
    41  	if opts.TrackerName != "" {
    42  		m["TrackerName"] = opts.TrackerName
    43  	}
    44  	if opts.BucketName != "" {
    45  		m["BucketName"] = opts.BucketName
    46  	}
    47  	if opts.FilePrefixName != "" {
    48  		m["FilePrefixName"] = opts.FilePrefixName
    49  	}
    50  	if opts.Status != "" {
    51  		m["Status"] = opts.Status
    52  	}
    53  
    54  	if len(m) > 0 && len(tracker) > 0 {
    55  		for _, trackers := range tracker {
    56  			matched = true
    57  
    58  			for key, value := range m {
    59  				if sVal := getStructField(&trackers, key); !(sVal == value) {
    60  					matched = false
    61  				}
    62  			}
    63  
    64  			if matched {
    65  				refinedTracker = append(refinedTracker, trackers)
    66  			}
    67  		}
    68  	} else {
    69  		refinedTracker = tracker
    70  	}
    71  	return refinedTracker, nil
    72  }
    73  
    74  func getStructField(v *Tracker, field string) string {
    75  	r := reflect.ValueOf(v)
    76  	f := reflect.Indirect(r).FieldByName(field)
    77  	return string(f.String())
    78  }
    79  
    80  // CreateOptsBuilder allows extensions to add additional parameters to the
    81  // Create request.
    82  type CreateOptsBuilder interface {
    83  	ToTrackerCreateMap() (map[string]interface{}, error)
    84  }
    85  
    86  // CreateOptsWithSMN contains the options for create a Tracker. This object is
    87  // passed to tracker.Create().
    88  type CreateOptsWithSMN struct {
    89  	BucketName                string                    `json:"bucket_name" required:"true"`
    90  	FilePrefixName            string                    `json:"file_prefix_name,omitempty"`
    91  	SimpleMessageNotification SimpleMessageNotification `json:"smn,omitempty"`
    92  }
    93  
    94  // CreateOpts contains the options for create a Tracker. This object is
    95  // passed to tracker.Create().
    96  type CreateOpts struct {
    97  	BucketName     string `json:"bucket_name" required:"true"`
    98  	FilePrefixName string `json:"file_prefix_name,omitempty"`
    99  }
   100  
   101  type SimpleMessageNotification struct {
   102  	IsSupportSMN          bool     `json:"is_support_smn"`
   103  	TopicID               string   `json:"topic_id"`
   104  	Operations            []string `json:"operations" required:"true"`
   105  	IsSendAllKeyOperation bool     `json:"is_send_all_key_operation"`
   106  	NeedNotifyUserList    []string `json:"need_notify_user_list,omitempty"`
   107  }
   108  
   109  // ToTrackerCreateMap assembles a request body based on the contents of a
   110  // CreateOpts.
   111  func (opts CreateOpts) ToTrackerCreateMap() (map[string]interface{}, error) {
   112  	return golangsdk.BuildRequestBody(opts, "")
   113  }
   114  
   115  // ToTrackerCreateMap assembles a request body based on the contents of a
   116  // CreateOpts.
   117  func (opts CreateOptsWithSMN) ToTrackerCreateMap() (map[string]interface{}, error) {
   118  	return golangsdk.BuildRequestBody(opts, "")
   119  }
   120  
   121  // Create will create a new tracker based on the values in CreateOpts. To extract
   122  // the tracker name  from the response, call the Extract method on the
   123  // CreateResult.
   124  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   125  	b, err := opts.ToTrackerCreateMap()
   126  
   127  	if err != nil {
   128  		r.Err = err
   129  		return
   130  	}
   131  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
   132  		OkCodes: []int{201},
   133  	})
   134  	return
   135  }
   136  
   137  // UpdateOptsWithSMN contains all the values needed to update a  tracker
   138  type UpdateOptsWithSMN struct {
   139  	Status                    string                    `json:"status,omitempty"`
   140  	BucketName                string                    `json:"bucket_name" required:"true"`
   141  	FilePrefixName            string                    `json:"file_prefix_name,omitempty"`
   142  	SimpleMessageNotification SimpleMessageNotification `json:"smn,omitempty"`
   143  }
   144  
   145  // UpdateOpts contains all the values needed to update a  tracker
   146  type UpdateOpts struct {
   147  	Status         string `json:"status,omitempty"`
   148  	BucketName     string `json:"bucket_name" required:"true"`
   149  	FilePrefixName string `json:"file_prefix_name,omitempty"`
   150  }
   151  
   152  // UpdateOptsBuilder allows extensions to add additional parameters to the
   153  // Update request.
   154  type UpdateOptsBuilder interface {
   155  	ToTrackerUpdateMap() (map[string]interface{}, error)
   156  }
   157  
   158  // ToTrackerUpdateMap builds an update body based on UpdateOpts.
   159  func (opts UpdateOptsWithSMN) ToTrackerUpdateMap() (map[string]interface{}, error) {
   160  	return golangsdk.BuildRequestBody(opts, "")
   161  }
   162  
   163  func (opts UpdateOpts) ToTrackerUpdateMap() (map[string]interface{}, error) {
   164  	return golangsdk.BuildRequestBody(opts, "")
   165  }
   166  
   167  func Update(client *golangsdk.ServiceClient, opts UpdateOptsBuilder) (r UpdateResult) {
   168  	b, err := opts.ToTrackerUpdateMap()
   169  	if err != nil {
   170  		r.Err = err
   171  		return
   172  	}
   173  	_, r.Err = client.Put(resourceURL(client), b, &r.Body, &golangsdk.RequestOpts{
   174  		OkCodes: []int{200},
   175  	})
   176  	return
   177  }
   178  
   179  // Delete will permanently delete a particular tracker.
   180  func Delete(client *golangsdk.ServiceClient) (r DeleteResult) {
   181  	_, r.Err = client.Delete(rootURL(client), &golangsdk.RequestOpts{
   182  		OkCodes:  []int{204},
   183  		JSONBody: nil,
   184  	})
   185  	return
   186  }