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