github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/csbs/v1/policies/results.go (about)

     1  package policies
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk"
    10  	"github.com/huaweicloud/golangsdk/pagination"
    11  )
    12  
    13  type BackupPolicy struct {
    14  	CreatedAt           time.Time                `json:"-"`
    15  	Description         string                   `json:"description"`
    16  	ID                  string                   `json:"id"`
    17  	Name                string                   `json:"name"`
    18  	Parameters          PolicyParam              `json:"parameters"`
    19  	ProjectId           string                   `json:"project_id"`
    20  	ProviderId          string                   `json:"provider_id"`
    21  	Resources           []Resource               `json:"resources"`
    22  	ScheduledOperations []ScheduledOperationResp `json:"scheduled_operations"`
    23  	Status              string                   `json:"status"`
    24  	Tags                []ResourceTag            `json:"tags"`
    25  }
    26  
    27  type ScheduledOperationResp struct {
    28  	Description         string                  `json:"description"`
    29  	Enabled             bool                    `json:"enabled"`
    30  	Name                string                  `json:"name"`
    31  	OperationType       string                  `json:"operation_type"`
    32  	OperationDefinition OperationDefinitionResp `json:"operation_definition"`
    33  	Trigger             TriggerResp             `json:"trigger"`
    34  	ID                  string                  `json:"id"`
    35  	TriggerID           string                  `json:"trigger_id"`
    36  }
    37  
    38  type OperationDefinitionResp struct {
    39  	MaxBackups            int    `json:"-"`
    40  	RetentionDurationDays int    `json:"-"`
    41  	Permanent             bool   `json:"-"`
    42  	PlanId                string `json:"plan_id"`
    43  	ProviderId            string `json:"provider_id"`
    44  }
    45  
    46  type TriggerResp struct {
    47  	Properties TriggerPropertiesResp `json:"properties"`
    48  	Name       string                `json:"name"`
    49  	ID         string                `json:"id"`
    50  	Type       string                `json:"type"`
    51  }
    52  
    53  type TriggerPropertiesResp struct {
    54  	Pattern   string    `json:"pattern"`
    55  	StartTime time.Time `json:"-"`
    56  }
    57  
    58  // UnmarshalJSON helps to unmarshal BackupPolicy fields into needed values.
    59  func (r *BackupPolicy) UnmarshalJSON(b []byte) error {
    60  	type tmp BackupPolicy
    61  	var s struct {
    62  		tmp
    63  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    64  	}
    65  	err := json.Unmarshal(b, &s)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	*r = BackupPolicy(s.tmp)
    70  
    71  	r.CreatedAt = time.Time(s.CreatedAt)
    72  
    73  	return err
    74  }
    75  
    76  // UnmarshalJSON helps to unmarshal TriggerPropertiesResp fields into needed values.
    77  func (r *TriggerPropertiesResp) UnmarshalJSON(b []byte) error {
    78  	type tmp TriggerPropertiesResp
    79  	var s struct {
    80  		tmp
    81  		StartTime golangsdk.JSONRFC3339ZNoTNoZ `json:"start_time"`
    82  	}
    83  	err := json.Unmarshal(b, &s)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	*r = TriggerPropertiesResp(s.tmp)
    88  
    89  	r.StartTime = time.Time(s.StartTime)
    90  
    91  	return err
    92  }
    93  
    94  // UnmarshalJSON helps to unmarshal OperationDefinitionResp fields into needed values.
    95  func (r *OperationDefinitionResp) UnmarshalJSON(b []byte) error {
    96  	type tmp OperationDefinitionResp
    97  	var s struct {
    98  		tmp
    99  		MaxBackups            string `json:"max_backups"`
   100  		RetentionDurationDays string `json:"retention_duration_days"`
   101  		Permanent             string `json:"permanent"`
   102  	}
   103  
   104  	err := json.Unmarshal(b, &s)
   105  
   106  	if err != nil {
   107  		switch err.(type) {
   108  		case *json.UnmarshalTypeError:
   109  			// handle type error to keep compatible with multi types
   110  			var s struct {
   111  				tmp
   112  				MaxBackups            int  `json:"max_backups"`
   113  				RetentionDurationDays int  `json:"retention_duration_days"`
   114  				Permanent             bool `json:"permanent"`
   115  			}
   116  			err := json.Unmarshal(b, &s)
   117  			if err != nil {
   118  				return err
   119  			}
   120  			*r = OperationDefinitionResp(s.tmp)
   121  			r.MaxBackups = s.MaxBackups
   122  			r.RetentionDurationDays = s.RetentionDurationDays
   123  			r.Permanent = s.Permanent
   124  			return nil
   125  		default:
   126  			return err
   127  		}
   128  	}
   129  
   130  	*r = OperationDefinitionResp(s.tmp)
   131  
   132  	switch s.MaxBackups {
   133  	case "":
   134  		r.MaxBackups = 0
   135  	default:
   136  		r.MaxBackups, err = strconv.Atoi(s.MaxBackups)
   137  		if err != nil {
   138  			return err
   139  		}
   140  	}
   141  
   142  	switch s.RetentionDurationDays {
   143  	case "":
   144  		r.RetentionDurationDays = 0
   145  	default:
   146  		r.RetentionDurationDays, err = strconv.Atoi(s.RetentionDurationDays)
   147  		if err != nil {
   148  			return err
   149  		}
   150  	}
   151  
   152  	switch s.Permanent {
   153  	case "":
   154  		r.Permanent = false
   155  	default:
   156  		r.Permanent, err = strconv.ParseBool(s.Permanent)
   157  		if err != nil {
   158  			return err
   159  		}
   160  	}
   161  
   162  	return err
   163  }
   164  
   165  // Extract will get the backup policies object from the commonResult
   166  func (r commonResult) Extract() (*BackupPolicy, error) {
   167  	var s struct {
   168  		BackupPolicy *BackupPolicy `json:"policy"`
   169  	}
   170  
   171  	err := r.ExtractInto(&s)
   172  	return s.BackupPolicy, err
   173  }
   174  
   175  // BackupPolicyPage is the page returned by a pager when traversing over a
   176  // collection of backup policies.
   177  type BackupPolicyPage struct {
   178  	pagination.LinkedPageBase
   179  }
   180  
   181  // NextPageURL is invoked when a paginated collection of backup policies has reached
   182  // the end of a page and the pager seeks to traverse over a new one. In order
   183  // to do this, it needs to construct the next page's URL.
   184  func (r BackupPolicyPage) NextPageURL() (string, error) {
   185  	var s struct {
   186  		Links []golangsdk.Link `json:"policies_links"`
   187  	}
   188  	err := r.ExtractInto(&s)
   189  	if err != nil {
   190  		return "", err
   191  	}
   192  	return golangsdk.ExtractNextURL(s.Links)
   193  }
   194  
   195  // IsEmpty checks whether a BackupPolicyPage struct is empty.
   196  func (r BackupPolicyPage) IsEmpty() (bool, error) {
   197  	is, err := ExtractBackupPolicies(r)
   198  	return len(is) == 0, err
   199  }
   200  
   201  // ExtractBackupPolicies accepts a Page struct, specifically a BackupPolicyPage struct,
   202  // and extracts the elements into a slice of Policy structs. In other words,
   203  // a generic collection is mapped into a relevant slice.
   204  func ExtractBackupPolicies(r pagination.Page) ([]BackupPolicy, error) {
   205  	var s struct {
   206  		BackupPolicies []BackupPolicy `json:"policies"`
   207  	}
   208  	err := (r.(BackupPolicyPage)).ExtractInto(&s)
   209  	return s.BackupPolicies, err
   210  }
   211  
   212  type commonResult struct {
   213  	golangsdk.Result
   214  }
   215  
   216  type CreateResult struct {
   217  	commonResult
   218  }
   219  
   220  type GetResult struct {
   221  	commonResult
   222  }
   223  
   224  type DeleteResult struct {
   225  	commonResult
   226  }
   227  
   228  type UpdateResult struct {
   229  	commonResult
   230  }
   231  
   232  type ListResult struct {
   233  	commonResult
   234  }