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

     1  package routers
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // GatewayInfo represents the information of an external gateway for any
    12  // particular network router.
    13  type GatewayInfo struct {
    14  	NetworkID        string            `json:"network_id,omitempty"`
    15  	EnableSNAT       *bool             `json:"enable_snat,omitempty"`
    16  	ExternalFixedIPs []ExternalFixedIP `json:"external_fixed_ips,omitempty"`
    17  }
    18  
    19  // ExternalFixedIP is the IP address and subnet ID of the external gateway of a
    20  // router.
    21  type ExternalFixedIP struct {
    22  	IPAddress string `json:"ip_address,omitempty"`
    23  	SubnetID  string `json:"subnet_id"`
    24  }
    25  
    26  // Route is a possible route in a router.
    27  type Route struct {
    28  	NextHop         string `json:"nexthop"`
    29  	DestinationCIDR string `json:"destination"`
    30  }
    31  
    32  // Router represents a Neutron router. A router is a logical entity that
    33  // forwards packets across internal subnets and NATs (network address
    34  // translation) them on external networks through an appropriate gateway.
    35  //
    36  // A router has an interface for each subnet with which it is associated. By
    37  // default, the IP address of such interface is the subnet's gateway IP. Also,
    38  // whenever a router is associated with a subnet, a port for that router
    39  // interface is added to the subnet's network.
    40  type Router struct {
    41  	// Status indicates whether or not a router is currently operational.
    42  	Status string `json:"status"`
    43  
    44  	// GateayInfo provides information on external gateway for the router.
    45  	GatewayInfo GatewayInfo `json:"external_gateway_info"`
    46  
    47  	// AdminStateUp is the administrative state of the router.
    48  	AdminStateUp bool `json:"admin_state_up"`
    49  
    50  	// Distributed is whether router is disitrubted or not.
    51  	Distributed bool `json:"distributed"`
    52  
    53  	// Name is the human readable name for the router. It does not have to be
    54  	// unique.
    55  	Name string `json:"name"`
    56  
    57  	// Description for the router.
    58  	Description string `json:"description"`
    59  
    60  	// ID is the unique identifier for the router.
    61  	ID string `json:"id"`
    62  
    63  	// TenantID is the project owner of the router. Only admin users can
    64  	// specify a project identifier other than its own.
    65  	TenantID string `json:"tenant_id"`
    66  
    67  	// ProjectID is the project owner of the router.
    68  	ProjectID string `json:"project_id"`
    69  
    70  	// Routes are a collection of static routes that the router will host.
    71  	Routes []Route `json:"routes"`
    72  
    73  	// Availability zone hints groups network nodes that run services like DHCP, L3, FW, and others.
    74  	// Used to make network resources highly available.
    75  	AvailabilityZoneHints []string `json:"availability_zone_hints"`
    76  
    77  	// Tags optionally set via extensions/attributestags
    78  	Tags []string `json:"tags"`
    79  }
    80  
    81  // RouterPage is the page returned by a pager when traversing over a
    82  // collection of routers.
    83  type RouterPage struct {
    84  	pagination.LinkedPageBase
    85  }
    86  
    87  // NextPageURL is invoked when a paginated collection of routers has reached
    88  // the end of a page and the pager seeks to traverse over a new one. In order
    89  // to do this, it needs to construct the next page's URL.
    90  func (r RouterPage) NextPageURL() (string, error) {
    91  	var s struct {
    92  		Links []gophercloud.Link `json:"routers_links"`
    93  	}
    94  	err := r.ExtractInto(&s)
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  	return gophercloud.ExtractNextURL(s.Links)
    99  }
   100  
   101  // IsEmpty checks whether a RouterPage struct is empty.
   102  func (r RouterPage) IsEmpty() (bool, error) {
   103  	if r.StatusCode == 204 {
   104  		return true, nil
   105  	}
   106  
   107  	is, err := ExtractRouters(r)
   108  	return len(is) == 0, err
   109  }
   110  
   111  // ExtractRouters accepts a Page struct, specifically a RouterPage struct,
   112  // and extracts the elements into a slice of Router structs. In other words,
   113  // a generic collection is mapped into a relevant slice.
   114  func ExtractRouters(r pagination.Page) ([]Router, error) {
   115  	var s struct {
   116  		Routers []Router `json:"routers"`
   117  	}
   118  	err := (r.(RouterPage)).ExtractInto(&s)
   119  	return s.Routers, err
   120  }
   121  
   122  type commonResult struct {
   123  	gophercloud.Result
   124  }
   125  
   126  // Extract is a function that accepts a result and extracts a router.
   127  func (r commonResult) Extract() (*Router, error) {
   128  	var s struct {
   129  		Router *Router `json:"router"`
   130  	}
   131  	err := r.ExtractInto(&s)
   132  	return s.Router, err
   133  }
   134  
   135  // CreateResult represents the result of a create operation. Call its Extract
   136  // method to interpret it as a Router.
   137  type CreateResult struct {
   138  	commonResult
   139  }
   140  
   141  // GetResult represents the result of a get operation. Call its Extract
   142  // method to interpret it as a Router.
   143  type GetResult struct {
   144  	commonResult
   145  }
   146  
   147  // UpdateResult represents the result of an update operation. Call its Extract
   148  // method to interpret it as a Router.
   149  type UpdateResult struct {
   150  	commonResult
   151  }
   152  
   153  // DeleteResult represents the result of a delete operation. Call its ExtractErr
   154  // method to determine if the request succeeded or failed.
   155  type DeleteResult struct {
   156  	gophercloud.ErrResult
   157  }
   158  
   159  // InterfaceInfo represents information about a particular router interface. As
   160  // mentioned above, in order for a router to forward to a subnet, it needs an
   161  // interface.
   162  type InterfaceInfo struct {
   163  	// SubnetID is the ID of the subnet which this interface is associated with.
   164  	SubnetID string `json:"subnet_id"`
   165  
   166  	// PortID is the ID of the port that is a part of the subnet.
   167  	PortID string `json:"port_id"`
   168  
   169  	// ID is the UUID of the interface.
   170  	ID string `json:"id"`
   171  
   172  	// TenantID is the owner of the interface.
   173  	TenantID string `json:"tenant_id"`
   174  }
   175  
   176  // InterfaceResult represents the result of interface operations, such as
   177  // AddInterface() and RemoveInterface(). Call its Extract method to interpret
   178  // the result as a InterfaceInfo.
   179  type InterfaceResult struct {
   180  	gophercloud.Result
   181  }
   182  
   183  // Extract is a function that accepts a result and extracts an information struct.
   184  func (r InterfaceResult) Extract() (*InterfaceInfo, error) {
   185  	var s InterfaceInfo
   186  	err := r.ExtractInto(&s)
   187  	return &s, err
   188  }
   189  
   190  // L3Agent represents a Neutron agent for routers.
   191  type L3Agent struct {
   192  	// ID is the id of the agent.
   193  	ID string `json:"id"`
   194  
   195  	// AdminStateUp is an administrative state of the agent.
   196  	AdminStateUp bool `json:"admin_state_up"`
   197  
   198  	// AgentType is a type of the agent.
   199  	AgentType string `json:"agent_type"`
   200  
   201  	// Alive indicates whether agent is alive or not.
   202  	Alive bool `json:"alive"`
   203  
   204  	// ResourcesSynced indicates whether agent is synced or not.
   205  	// Not all agent types track resources via Placement.
   206  	ResourcesSynced bool `json:"resources_synced"`
   207  
   208  	// AvailabilityZone is a zone of the agent.
   209  	AvailabilityZone string `json:"availability_zone"`
   210  
   211  	// Binary is an executable binary of the agent.
   212  	Binary string `json:"binary"`
   213  
   214  	// Configurations is a configuration specific key/value pairs that are
   215  	// determined by the agent binary and type.
   216  	Configurations map[string]interface{} `json:"configurations"`
   217  
   218  	// CreatedAt is a creation timestamp.
   219  	CreatedAt time.Time `json:"-"`
   220  
   221  	// StartedAt is a starting timestamp.
   222  	StartedAt time.Time `json:"-"`
   223  
   224  	// HeartbeatTimestamp is a last heartbeat timestamp.
   225  	HeartbeatTimestamp time.Time `json:"-"`
   226  
   227  	// Description contains agent description.
   228  	Description string `json:"description"`
   229  
   230  	// Host is a hostname of the agent system.
   231  	Host string `json:"host"`
   232  
   233  	// Topic contains name of AMQP topic.
   234  	Topic string `json:"topic"`
   235  
   236  	// HAState is a ha state of agent(active/standby) for router
   237  	HAState string `json:"ha_state"`
   238  
   239  	// ResourceVersions is a list agent known objects and version numbers
   240  	ResourceVersions map[string]interface{} `json:"resource_versions"`
   241  }
   242  
   243  // UnmarshalJSON helps to convert the timestamps into the time.Time type.
   244  func (r *L3Agent) UnmarshalJSON(b []byte) error {
   245  	type tmp L3Agent
   246  	var s struct {
   247  		tmp
   248  		CreatedAt          gophercloud.JSONRFC3339ZNoTNoZ `json:"created_at"`
   249  		StartedAt          gophercloud.JSONRFC3339ZNoTNoZ `json:"started_at"`
   250  		HeartbeatTimestamp gophercloud.JSONRFC3339ZNoTNoZ `json:"heartbeat_timestamp"`
   251  	}
   252  	err := json.Unmarshal(b, &s)
   253  	if err != nil {
   254  		return err
   255  	}
   256  	*r = L3Agent(s.tmp)
   257  
   258  	r.CreatedAt = time.Time(s.CreatedAt)
   259  	r.StartedAt = time.Time(s.StartedAt)
   260  	r.HeartbeatTimestamp = time.Time(s.HeartbeatTimestamp)
   261  
   262  	return nil
   263  }
   264  
   265  type ListL3AgentsPage struct {
   266  	pagination.SinglePageBase
   267  }
   268  
   269  func (r ListL3AgentsPage) IsEmpty() (bool, error) {
   270  	if r.StatusCode == 204 {
   271  		return true, nil
   272  	}
   273  
   274  	v, err := ExtractL3Agents(r)
   275  	return len(v) == 0, err
   276  }
   277  
   278  func ExtractL3Agents(r pagination.Page) ([]L3Agent, error) {
   279  	var s struct {
   280  		L3Agents []L3Agent `json:"agents"`
   281  	}
   282  
   283  	err := (r.(ListL3AgentsPage)).ExtractInto(&s)
   284  	return s.L3Agents, err
   285  }