github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/agents/results.go (about)

     1  package agents
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/bgp/speakers"
     9  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
    11  	"github.com/gophercloud/gophercloud/pagination"
    12  )
    13  
    14  type commonResult struct {
    15  	gophercloud.Result
    16  }
    17  
    18  // Extract is a function that accepts a result and extracts an agent resource.
    19  func (r commonResult) Extract() (*Agent, error) {
    20  	var s struct {
    21  		Agent *Agent `json:"agent"`
    22  	}
    23  	err := r.ExtractInto(&s)
    24  	return s.Agent, err
    25  }
    26  
    27  // GetResult represents the result of a get operation. Call its Extract
    28  // method to interpret it as an Agent.
    29  type GetResult struct {
    30  	commonResult
    31  }
    32  
    33  // UpdateResult represents the result of a get operation. Call its Extract
    34  // method to interpret it as an Agent.
    35  type UpdateResult struct {
    36  	commonResult
    37  }
    38  
    39  // DeleteResult represents the result of a delete operation. Call its
    40  // ExtractErr method to determine if the request succeeded or failed.
    41  type DeleteResult struct {
    42  	gophercloud.ErrResult
    43  }
    44  
    45  // ScheduleDHCPNetworkResult represents the result of a schedule a network to
    46  // a DHCP agent operation. ExtractErr method to determine if the request
    47  // succeeded or failed.
    48  type ScheduleDHCPNetworkResult struct {
    49  	gophercloud.ErrResult
    50  }
    51  
    52  // RemoveDHCPNetworkResult represents the result of a remove a network from a
    53  // DHCP agent operation. ExtractErr method to determine if the request succeeded
    54  // or failed.
    55  type RemoveDHCPNetworkResult struct {
    56  	gophercloud.ErrResult
    57  }
    58  
    59  // ScheduleBGPSpeakerResult represents the result of adding a BGP speaker to a
    60  // BGP DR Agent. ExtractErr method to determine if the request succeeded or
    61  // failed.
    62  type ScheduleBGPSpeakerResult struct {
    63  	gophercloud.ErrResult
    64  }
    65  
    66  // RemoveBGPSpeakerResult represents the result of removing a BGP speaker from a
    67  // BGP DR Agent. ExtractErr method to determine if the request succeeded or
    68  // failed.
    69  type RemoveBGPSpeakerResult struct {
    70  	gophercloud.ErrResult
    71  }
    72  
    73  // Agent represents a Neutron agent.
    74  type Agent struct {
    75  	// ID is the id of the agent.
    76  	ID string `json:"id"`
    77  
    78  	// AdminStateUp is an administrative state of the agent.
    79  	AdminStateUp bool `json:"admin_state_up"`
    80  
    81  	// AgentType is a type of the agent.
    82  	AgentType string `json:"agent_type"`
    83  
    84  	// Alive indicates whether agent is alive or not.
    85  	Alive bool `json:"alive"`
    86  
    87  	// ResourcesSynced indicates whether agent is synced or not.
    88  	// Not all agent types track resources via Placement.
    89  	ResourcesSynced bool `json:"resources_synced"`
    90  
    91  	// AvailabilityZone is a zone of the agent.
    92  	AvailabilityZone string `json:"availability_zone"`
    93  
    94  	// Binary is an executable binary of the agent.
    95  	Binary string `json:"binary"`
    96  
    97  	// Configurations is a configuration specific key/value pairs that are
    98  	// determined by the agent binary and type.
    99  	Configurations map[string]interface{} `json:"configurations"`
   100  
   101  	// CreatedAt is a creation timestamp.
   102  	CreatedAt time.Time `json:"-"`
   103  
   104  	// StartedAt is a starting timestamp.
   105  	StartedAt time.Time `json:"-"`
   106  
   107  	// HeartbeatTimestamp is a last heartbeat timestamp.
   108  	HeartbeatTimestamp time.Time `json:"-"`
   109  
   110  	// Description contains agent description.
   111  	Description string `json:"description"`
   112  
   113  	// Host is a hostname of the agent system.
   114  	Host string `json:"host"`
   115  
   116  	// Topic contains name of AMQP topic.
   117  	Topic string `json:"topic"`
   118  }
   119  
   120  // UnmarshalJSON helps to convert the timestamps into the time.Time type.
   121  func (r *Agent) UnmarshalJSON(b []byte) error {
   122  	type tmp Agent
   123  	var s struct {
   124  		tmp
   125  		CreatedAt          gophercloud.JSONRFC3339ZNoTNoZ `json:"created_at"`
   126  		StartedAt          gophercloud.JSONRFC3339ZNoTNoZ `json:"started_at"`
   127  		HeartbeatTimestamp gophercloud.JSONRFC3339ZNoTNoZ `json:"heartbeat_timestamp"`
   128  	}
   129  	err := json.Unmarshal(b, &s)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	*r = Agent(s.tmp)
   134  
   135  	r.CreatedAt = time.Time(s.CreatedAt)
   136  	r.StartedAt = time.Time(s.StartedAt)
   137  	r.HeartbeatTimestamp = time.Time(s.HeartbeatTimestamp)
   138  
   139  	return nil
   140  }
   141  
   142  // AgentPage stores a single page of Agents from a List() API call.
   143  type AgentPage struct {
   144  	pagination.LinkedPageBase
   145  }
   146  
   147  // NextPageURL is invoked when a paginated collection of agent has
   148  // reached the end of a page and the pager seeks to traverse over a new one.
   149  // In order to do this, it needs to construct the next page's URL.
   150  func (r AgentPage) NextPageURL() (string, error) {
   151  	var s struct {
   152  		Links []gophercloud.Link `json:"agents_links"`
   153  	}
   154  	err := r.ExtractInto(&s)
   155  	if err != nil {
   156  		return "", err
   157  	}
   158  	return gophercloud.ExtractNextURL(s.Links)
   159  }
   160  
   161  // IsEmpty determines whether or not a AgentPage is empty.
   162  func (r AgentPage) IsEmpty() (bool, error) {
   163  	if r.StatusCode == 204 {
   164  		return true, nil
   165  	}
   166  
   167  	agents, err := ExtractAgents(r)
   168  	return len(agents) == 0, err
   169  }
   170  
   171  // ExtractAgents interprets the results of a single page from a List()
   172  // API call, producing a slice of Agents structs.
   173  func ExtractAgents(r pagination.Page) ([]Agent, error) {
   174  	var s struct {
   175  		Agents []Agent `json:"agents"`
   176  	}
   177  	err := (r.(AgentPage)).ExtractInto(&s)
   178  	return s.Agents, err
   179  }
   180  
   181  // ListDHCPNetworksResult is the response from a List operation.
   182  // Call its Extract method to interpret it as networks.
   183  type ListDHCPNetworksResult struct {
   184  	gophercloud.Result
   185  }
   186  
   187  // Extract interprets any ListDHCPNetworksResult as an array of networks.
   188  func (r ListDHCPNetworksResult) Extract() ([]networks.Network, error) {
   189  	var s struct {
   190  		Networks []networks.Network `json:"networks"`
   191  	}
   192  
   193  	err := r.ExtractInto(&s)
   194  	return s.Networks, err
   195  }
   196  
   197  // ListBGPSpeakersResult is the respone of agents/{id}/bgp-speakers
   198  type ListBGPSpeakersResult struct {
   199  	pagination.SinglePageBase
   200  }
   201  
   202  func (r ListBGPSpeakersResult) IsEmpty() (bool, error) {
   203  	if r.StatusCode == 204 {
   204  		return true, nil
   205  	}
   206  
   207  	speakers, err := ExtractBGPSpeakers(r)
   208  	return 0 == len(speakers), err
   209  }
   210  
   211  // ExtractBGPSpeakers inteprets the ListBGPSpeakersResult into an array of BGP speakers
   212  func ExtractBGPSpeakers(r pagination.Page) ([]speakers.BGPSpeaker, error) {
   213  	var s struct {
   214  		Speakers []speakers.BGPSpeaker `json:"bgp_speakers"`
   215  	}
   216  
   217  	err := (r.(ListBGPSpeakersResult)).ExtractInto(&s)
   218  	return s.Speakers, err
   219  }
   220  
   221  // ListL3RoutersResult is the response from a List operation.
   222  // Call its Extract method to interpret it as routers.
   223  type ListL3RoutersResult struct {
   224  	gophercloud.Result
   225  }
   226  
   227  // ScheduleL3RouterResult represents the result of a schedule a router to
   228  // a L3 agent operation. ExtractErr method to determine if the request
   229  // succeeded or failed.
   230  type ScheduleL3RouterResult struct {
   231  	gophercloud.ErrResult
   232  }
   233  
   234  // RemoveL3RouterResult represents the result of a remove a router from a
   235  // L3 agent operation. ExtractErr method to determine if the request succeeded
   236  // or failed.
   237  type RemoveL3RouterResult struct {
   238  	gophercloud.ErrResult
   239  }
   240  
   241  // Extract interprets any ListL3RoutesResult as an array of routers.
   242  func (r ListL3RoutersResult) Extract() ([]routers.Router, error) {
   243  	var s struct {
   244  		Routers []routers.Router `json:"routers"`
   245  	}
   246  
   247  	err := r.ExtractInto(&s)
   248  	return s.Routers, err
   249  }