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

     1  package configurations
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // Config represents a configuration group API resource.
    12  type Config struct {
    13  	Created              time.Time `json:"-"`
    14  	Updated              time.Time `json:"-"`
    15  	DatastoreName        string    `json:"datastore_name"`
    16  	DatastoreVersionID   string    `json:"datastore_version_id"`
    17  	DatastoreVersionName string    `json:"datastore_version_name"`
    18  	Description          string
    19  	ID                   string
    20  	Name                 string
    21  	Values               map[string]interface{}
    22  }
    23  
    24  func (r *Config) UnmarshalJSON(b []byte) error {
    25  	type tmp Config
    26  	var s struct {
    27  		tmp
    28  		Created golangsdk.JSONRFC3339NoZ `json:"created"`
    29  		Updated golangsdk.JSONRFC3339NoZ `json:"updated"`
    30  	}
    31  	err := json.Unmarshal(b, &s)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	*r = Config(s.tmp)
    36  
    37  	r.Created = time.Time(s.Created)
    38  	r.Updated = time.Time(s.Updated)
    39  
    40  	return nil
    41  }
    42  
    43  // ConfigPage contains a page of Config resources in a paginated collection.
    44  type ConfigPage struct {
    45  	pagination.SinglePageBase
    46  }
    47  
    48  // IsEmpty indicates whether a ConfigPage is empty.
    49  func (r ConfigPage) IsEmpty() (bool, error) {
    50  	is, err := ExtractConfigs(r)
    51  	return len(is) == 0, err
    52  }
    53  
    54  // ExtractConfigs will retrieve a slice of Config structs from a page.
    55  func ExtractConfigs(r pagination.Page) ([]Config, error) {
    56  	var s struct {
    57  		Configs []Config `json:"configurations"`
    58  	}
    59  	err := (r.(ConfigPage)).ExtractInto(&s)
    60  	return s.Configs, err
    61  }
    62  
    63  type commonResult struct {
    64  	golangsdk.Result
    65  }
    66  
    67  // Extract will retrieve a Config resource from an operation result.
    68  func (r commonResult) Extract() (*Config, error) {
    69  	var s struct {
    70  		Config *Config `json:"configuration"`
    71  	}
    72  	err := r.ExtractInto(&s)
    73  	return s.Config, err
    74  }
    75  
    76  // GetResult represents the result of a Get operation.
    77  type GetResult struct {
    78  	commonResult
    79  }
    80  
    81  // CreateResult represents the result of a Create operation.
    82  type CreateResult struct {
    83  	commonResult
    84  }
    85  
    86  // UpdateResult represents the result of an Update operation.
    87  type UpdateResult struct {
    88  	golangsdk.ErrResult
    89  }
    90  
    91  // ReplaceResult represents the result of a Replace operation.
    92  type ReplaceResult struct {
    93  	golangsdk.ErrResult
    94  }
    95  
    96  // DeleteResult represents the result of a Delete operation.
    97  type DeleteResult struct {
    98  	golangsdk.ErrResult
    99  }
   100  
   101  // Param represents a configuration parameter API resource.
   102  type Param struct {
   103  	Max             float64
   104  	Min             float64
   105  	Name            string
   106  	RestartRequired bool `json:"restart_required"`
   107  	Type            string
   108  }
   109  
   110  // ParamPage contains a page of Param resources in a paginated collection.
   111  type ParamPage struct {
   112  	pagination.SinglePageBase
   113  }
   114  
   115  // IsEmpty indicates whether a ParamPage is empty.
   116  func (r ParamPage) IsEmpty() (bool, error) {
   117  	is, err := ExtractParams(r)
   118  	return len(is) == 0, err
   119  }
   120  
   121  // ExtractParams will retrieve a slice of Param structs from a page.
   122  func ExtractParams(r pagination.Page) ([]Param, error) {
   123  	var s struct {
   124  		Params []Param `json:"configuration-parameters"`
   125  	}
   126  	err := (r.(ParamPage)).ExtractInto(&s)
   127  	return s.Params, err
   128  }
   129  
   130  // ParamResult represents the result of an operation which retrieves details
   131  // about a particular configuration param.
   132  type ParamResult struct {
   133  	golangsdk.Result
   134  }
   135  
   136  // Extract will retrieve a param from an operation result.
   137  func (r ParamResult) Extract() (*Param, error) {
   138  	var s *Param
   139  	err := r.ExtractInto(&s)
   140  	return s, err
   141  }