github.com/gophercloud/gophercloud@v1.11.0/openstack/db/v1/instances/results.go (about)

     1  package instances
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/openstack/db/v1/datastores"
     9  	"github.com/gophercloud/gophercloud/openstack/db/v1/users"
    10  	"github.com/gophercloud/gophercloud/pagination"
    11  )
    12  
    13  // Volume represents information about an attached volume for a database instance.
    14  type Volume struct {
    15  	// The size in GB of the volume
    16  	Size int
    17  
    18  	Used float64
    19  }
    20  
    21  // Flavor represents (virtual) hardware configurations for server resources in a region.
    22  type Flavor struct {
    23  	// The flavor's unique identifier.
    24  	ID string
    25  	// Links to access the flavor.
    26  	Links []gophercloud.Link
    27  }
    28  
    29  // Fault describes the fault reason in more detail when a database instance has errored
    30  type Fault struct {
    31  	// Indicates the time when the fault occured
    32  	Created time.Time `json:"-"`
    33  
    34  	// A message describing the fault reason
    35  	Message string
    36  
    37  	// More details about the fault, for example a stack trace. Only filled
    38  	// in for admin users.
    39  	Details string
    40  }
    41  
    42  // Address represents the IP address and its type to connect with the instance.
    43  type Address struct {
    44  	// The address type, e.g public
    45  	Type string
    46  
    47  	// The actual IP address
    48  	Address string
    49  }
    50  
    51  func (r *Fault) UnmarshalJSON(b []byte) error {
    52  	type tmp Fault
    53  	var s struct {
    54  		tmp
    55  		Created gophercloud.JSONRFC3339NoZ `json:"created"`
    56  	}
    57  	err := json.Unmarshal(b, &s)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	*r = Fault(s.tmp)
    62  
    63  	r.Created = time.Time(s.Created)
    64  
    65  	return nil
    66  }
    67  
    68  // Instance represents a remote MySQL instance.
    69  type Instance struct {
    70  	// Indicates the datetime that the instance was created
    71  	Created time.Time `json:"-"`
    72  
    73  	// Indicates the most recent datetime that the instance was updated.
    74  	Updated time.Time `json:"-"`
    75  
    76  	// Indicates the hardware flavor the instance uses.
    77  	Flavor Flavor
    78  
    79  	// A DNS-resolvable hostname associated with the database instance (rather
    80  	// than an IPv4 address). Since the hostname always resolves to the correct
    81  	// IP address of the database instance, this relieves the user from the task
    82  	// of maintaining the mapping. Note that although the IP address may likely
    83  	// change on resizing, migrating, and so forth, the hostname always resolves
    84  	// to the correct database instance.
    85  	Hostname string
    86  
    87  	// The IP addresses associated with the database instance
    88  	// Is empty if the instance has a hostname.
    89  	// Deprecated in favor of Addresses.
    90  	IP []string
    91  
    92  	// Indicates the unique identifier for the instance resource.
    93  	ID string
    94  
    95  	// Exposes various links that reference the instance resource.
    96  	Links []gophercloud.Link
    97  
    98  	// The human-readable name of the instance.
    99  	Name string
   100  
   101  	// The build status of the instance.
   102  	Status string
   103  
   104  	// Fault information (only available when the instance has errored)
   105  	Fault *Fault
   106  
   107  	// Information about the attached volume of the instance.
   108  	Volume Volume
   109  
   110  	// Indicates how the instance stores data.
   111  	Datastore datastores.DatastorePartial
   112  
   113  	// The instance addresses
   114  	Addresses []Address
   115  }
   116  
   117  func (r *Instance) UnmarshalJSON(b []byte) error {
   118  	type tmp Instance
   119  	var s struct {
   120  		tmp
   121  		Created gophercloud.JSONRFC3339NoZ `json:"created"`
   122  		Updated gophercloud.JSONRFC3339NoZ `json:"updated"`
   123  	}
   124  	err := json.Unmarshal(b, &s)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	*r = Instance(s.tmp)
   129  
   130  	r.Created = time.Time(s.Created)
   131  	r.Updated = time.Time(s.Updated)
   132  
   133  	return nil
   134  }
   135  
   136  type commonResult struct {
   137  	gophercloud.Result
   138  }
   139  
   140  // CreateResult represents the result of a Create operation.
   141  type CreateResult struct {
   142  	commonResult
   143  }
   144  
   145  // GetResult represents the result of a Get operation.
   146  type GetResult struct {
   147  	commonResult
   148  }
   149  
   150  // DeleteResult represents the result of a Delete operation.
   151  type DeleteResult struct {
   152  	gophercloud.ErrResult
   153  }
   154  
   155  // ConfigurationResult represents the result of a AttachConfigurationGroup/DetachConfigurationGroup operation.
   156  type ConfigurationResult struct {
   157  	gophercloud.ErrResult
   158  }
   159  
   160  // Extract will extract an Instance from various result structs.
   161  func (r commonResult) Extract() (*Instance, error) {
   162  	var s struct {
   163  		Instance *Instance `json:"instance"`
   164  	}
   165  	err := r.ExtractInto(&s)
   166  	return s.Instance, err
   167  }
   168  
   169  // InstancePage represents a single page of a paginated instance collection.
   170  type InstancePage struct {
   171  	pagination.LinkedPageBase
   172  }
   173  
   174  // IsEmpty checks to see whether the collection is empty.
   175  func (page InstancePage) IsEmpty() (bool, error) {
   176  	if page.StatusCode == 204 {
   177  		return true, nil
   178  	}
   179  
   180  	instances, err := ExtractInstances(page)
   181  	return len(instances) == 0, err
   182  }
   183  
   184  // NextPageURL will retrieve the next page URL.
   185  func (page InstancePage) NextPageURL() (string, error) {
   186  	var s struct {
   187  		Links []gophercloud.Link `json:"instances_links"`
   188  	}
   189  	err := page.ExtractInto(&s)
   190  	if err != nil {
   191  		return "", err
   192  	}
   193  	return gophercloud.ExtractNextURL(s.Links)
   194  }
   195  
   196  // ExtractInstances will convert a generic pagination struct into a more
   197  // relevant slice of Instance structs.
   198  func ExtractInstances(r pagination.Page) ([]Instance, error) {
   199  	var s struct {
   200  		Instances []Instance `json:"instances"`
   201  	}
   202  	err := (r.(InstancePage)).ExtractInto(&s)
   203  	return s.Instances, err
   204  }
   205  
   206  // EnableRootUserResult represents the result of an operation to enable the root user.
   207  type EnableRootUserResult struct {
   208  	gophercloud.Result
   209  }
   210  
   211  // Extract will extract root user information from a UserRootResult.
   212  func (r EnableRootUserResult) Extract() (*users.User, error) {
   213  	var s struct {
   214  		User *users.User `json:"user"`
   215  	}
   216  	err := r.ExtractInto(&s)
   217  	return s.User, err
   218  }
   219  
   220  // ActionResult represents the result of action requests, such as: restarting
   221  // an instance service, resizing its memory allocation, and resizing its
   222  // attached volume size.
   223  type ActionResult struct {
   224  	gophercloud.ErrResult
   225  }
   226  
   227  // IsRootEnabledResult is the result of a call to IsRootEnabled. To see if
   228  // root is enabled, call the type's Extract method.
   229  type IsRootEnabledResult struct {
   230  	gophercloud.Result
   231  }
   232  
   233  // Extract is used to extract the data from a IsRootEnabledResult.
   234  func (r IsRootEnabledResult) Extract() (bool, error) {
   235  	return r.Body.(map[string]interface{})["rootEnabled"] == true, r.Err
   236  }