github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/csbs/v1/policies/results.go (about)

     1  package policies
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/opentelekomcloud/gophertelekomcloud"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
    12  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
    13  )
    14  
    15  type BackupPolicy struct {
    16  	// Creation time, for example, 2017-04-18T01:21:52.701973
    17  	CreatedAt time.Time `json:"-"`
    18  	// Backup policy description
    19  	// The value consists of 0 to 255 characters and must not contain a greater-than sign (>) or less-than sign (<).
    20  	Description string `json:"description"`
    21  	// Backup policy ID
    22  	ID string `json:"id"`
    23  	// Backup policy name
    24  	// The value consists of 1 to 255 characters and can contain only letters, digits, underscores (_), and hyphens (-).
    25  	Name string `json:"name"`
    26  	// Parameters of a backup policy
    27  	Parameters PolicyParam `json:"parameters"`
    28  	// Project ID
    29  	ProjectId string `json:"project_id"`
    30  	// Backup provider ID, which specifies whether the backup object is a server or disk.
    31  	// This parameter has a fixed value. For CSBS, the value is fc4d5750-22e7-4798-8a46-f48f62c4c1da.
    32  	ProviderId string `json:"provider_id"`
    33  	// Backup object list
    34  	Resources []Resource `json:"resources"`
    35  	// Scheduling period list
    36  	ScheduledOperations []ScheduledOperation `json:"-"`
    37  	// Backup policy status
    38  	// disabled: indicates that the backup policy is unavailable.
    39  	// enabled: indicates that the backup policy is available.
    40  	Status string `json:"status"`
    41  	// Tag list
    42  	// Keys in the tag list must be unique.
    43  	Tags []tags.ResourceTag `json:"tags"`
    44  }
    45  
    46  func extra(err error, raw *http.Response) (*BackupPolicy, error) {
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	var res BackupPolicy
    52  	err = extract.IntoStructPtr(raw.Body, &res, "policy")
    53  	return &res, err
    54  }
    55  
    56  type scheduledOperationString struct {
    57  	Description         string                    `json:"description"`
    58  	Enabled             bool                      `json:"enabled"`
    59  	Name                string                    `json:"name"`
    60  	OperationType       string                    `json:"operation_type"`
    61  	OperationDefinition operationDefinitionString `json:"operation_definition"`
    62  	Trigger             Trigger                   `json:"trigger" `
    63  	ID                  string                    `json:"id"`
    64  	TriggerID           string                    `json:"trigger_id"`
    65  }
    66  
    67  type operationDefinitionString struct {
    68  	MaxBackups            interface{} `json:"max_backups"`
    69  	RetentionDurationDays interface{} `json:"retention_duration_days"`
    70  	Permanent             interface{} `json:"permanent"`
    71  	PlanId                string      `json:"plan_id"`
    72  	ProviderId            string      `json:"provider_id"`
    73  	DayBackups            interface{} `json:"day_backups"`
    74  	WeekBackups           interface{} `json:"week_backups"`
    75  	MonthBackups          interface{} `json:"month_backups"`
    76  	YearBackups           interface{} `json:"year_backups"`
    77  	TimeZone              string      `json:"timezone"`
    78  }
    79  
    80  func toInt(v interface{}) int {
    81  	var i int
    82  
    83  	switch v := v.(type) {
    84  	case string:
    85  		i, _ = strconv.Atoi(v)
    86  	case float64:
    87  		i = int(v)
    88  	}
    89  
    90  	return i
    91  }
    92  
    93  // UnmarshalJSON helps to unmarshal BackupPolicy fields into needed values.
    94  func (r *BackupPolicy) UnmarshalJSON(b []byte) error {
    95  	type policy BackupPolicy
    96  	var tmp struct {
    97  		policy
    98  		CreatedAt           golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    99  		ScheduledOperations []scheduledOperationString    `json:"scheduled_operations"`
   100  	}
   101  
   102  	err := json.Unmarshal(b, &tmp)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	*r = BackupPolicy(tmp.policy)
   108  	r.CreatedAt = time.Time(tmp.CreatedAt)
   109  
   110  	for _, v := range tmp.ScheduledOperations {
   111  		def := v.OperationDefinition
   112  
   113  		var pt bool
   114  		switch p := def.Permanent.(type) {
   115  		case string:
   116  			pt, _ = strconv.ParseBool(p)
   117  		case bool:
   118  			pt = p
   119  		}
   120  
   121  		r.ScheduledOperations = append(r.ScheduledOperations, ScheduledOperation{
   122  			Description:   v.Description,
   123  			Enabled:       v.Enabled,
   124  			Name:          v.Name,
   125  			OperationType: v.OperationType,
   126  			OperationDefinition: OperationDefinition{
   127  				MaxBackups:            pointerto.Int(toInt(def.MaxBackups)),
   128  				RetentionDurationDays: toInt(def.RetentionDurationDays),
   129  				Permanent:             pt,
   130  				PlanId:                v.OperationDefinition.PlanId,
   131  				ProviderId:            v.OperationDefinition.ProviderId,
   132  				DayBackups:            toInt(def.DayBackups),
   133  				WeekBackups:           toInt(def.WeekBackups),
   134  				MonthBackups:          toInt(def.MonthBackups),
   135  				YearBackups:           toInt(def.YearBackups),
   136  				TimeZone:              v.OperationDefinition.TimeZone,
   137  			},
   138  			Trigger: Trigger{
   139  				Properties: v.Trigger.Properties,
   140  				Type:       v.Trigger.Type,
   141  			},
   142  			ID:        v.ID,
   143  			TriggerID: v.TriggerID,
   144  		})
   145  	}
   146  
   147  	return err
   148  }