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

     1  package agents
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToAgentListQuery() (string, error)
    12  }
    13  
    14  // ListOpts allows the filtering and sorting of paginated collections through
    15  // the Neutron API. Filtering is achieved by passing in struct field values
    16  // that map to the agent attributes you want to see returned.
    17  // SortKey allows you to sort by a particular agent attribute.
    18  // SortDir sets the direction, and is either `asc' or `desc'.
    19  // Marker and Limit are used for the pagination.
    20  type ListOpts struct {
    21  	ID               string `q:"id"`
    22  	AgentType        string `q:"agent_type"`
    23  	Alive            *bool  `q:"alive"`
    24  	AvailabilityZone string `q:"availability_zone"`
    25  	Binary           string `q:"binary"`
    26  	Description      string `q:"description"`
    27  	Host             string `q:"host"`
    28  	Topic            string `q:"topic"`
    29  	Limit            int    `q:"limit"`
    30  	Marker           string `q:"marker"`
    31  	SortKey          string `q:"sort_key"`
    32  	SortDir          string `q:"sort_dir"`
    33  }
    34  
    35  // ToAgentListQuery formats a ListOpts into a query string.
    36  func (opts ListOpts) ToAgentListQuery() (string, error) {
    37  	q, err := gophercloud.BuildQueryString(opts)
    38  	return q.String(), err
    39  }
    40  
    41  // List returns a Pager which allows you to iterate over a collection of
    42  // agents. It accepts a ListOpts struct, which allows you to filter and
    43  // sort the returned collection for greater efficiency.
    44  //
    45  // Default policy settings return only the agents owned by the project
    46  // of the user submitting the request, unless the user has the administrative
    47  // role.
    48  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    49  	url := listURL(c)
    50  	if opts != nil {
    51  		query, err := opts.ToAgentListQuery()
    52  		if err != nil {
    53  			return pagination.Pager{Err: err}
    54  		}
    55  		url += query
    56  	}
    57  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    58  		return AgentPage{pagination.LinkedPageBase{PageResult: r}}
    59  	})
    60  }
    61  
    62  // Get retrieves a specific agent based on its ID.
    63  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
    64  	resp, err := c.Get(getURL(c, id), &r.Body, nil)
    65  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    66  	return
    67  }
    68  
    69  // UpdateOptsBuilder allows extensions to add additional parameters to the
    70  // Update request.
    71  type UpdateOptsBuilder interface {
    72  	ToAgentUpdateMap() (map[string]interface{}, error)
    73  }
    74  
    75  // UpdateOpts represents the attributes used when updating an existing agent.
    76  type UpdateOpts struct {
    77  	Description  *string `json:"description,omitempty"`
    78  	AdminStateUp *bool   `json:"admin_state_up,omitempty"`
    79  }
    80  
    81  // ToAgentUpdateMap builds a request body from UpdateOpts.
    82  func (opts UpdateOpts) ToAgentUpdateMap() (map[string]interface{}, error) {
    83  	return gophercloud.BuildRequestBody(opts, "agent")
    84  }
    85  
    86  // Update updates a specific agent based on its ID.
    87  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    88  	b, err := opts.ToAgentUpdateMap()
    89  	if err != nil {
    90  		r.Err = err
    91  		return
    92  	}
    93  	resp, err := c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
    94  		OkCodes: []int{200},
    95  	})
    96  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    97  	return
    98  }
    99  
   100  // Delete deletes a specific agent based on its ID.
   101  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   102  	resp, err := c.Delete(getURL(c, id), nil)
   103  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   104  	return
   105  }
   106  
   107  // ListDHCPNetworks returns a list of networks scheduled to a specific
   108  // dhcp agent.
   109  func ListDHCPNetworks(c *gophercloud.ServiceClient, id string) (r ListDHCPNetworksResult) {
   110  	resp, err := c.Get(listDHCPNetworksURL(c, id), &r.Body, nil)
   111  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   112  	return
   113  }
   114  
   115  // ScheduleDHCPNetworkOptsBuilder allows extensions to add additional parameters
   116  // to the ScheduleDHCPNetwork request.
   117  type ScheduleDHCPNetworkOptsBuilder interface {
   118  	ToAgentScheduleDHCPNetworkMap() (map[string]interface{}, error)
   119  }
   120  
   121  // ScheduleDHCPNetworkOpts represents the attributes used when scheduling a
   122  // network to a DHCP agent.
   123  type ScheduleDHCPNetworkOpts struct {
   124  	NetworkID string `json:"network_id" required:"true"`
   125  }
   126  
   127  // ToAgentScheduleDHCPNetworkMap builds a request body from ScheduleDHCPNetworkOpts.
   128  func (opts ScheduleDHCPNetworkOpts) ToAgentScheduleDHCPNetworkMap() (map[string]interface{}, error) {
   129  	return gophercloud.BuildRequestBody(opts, "")
   130  }
   131  
   132  // ScheduleDHCPNetwork schedule a network to a DHCP agent.
   133  func ScheduleDHCPNetwork(c *gophercloud.ServiceClient, id string, opts ScheduleDHCPNetworkOptsBuilder) (r ScheduleDHCPNetworkResult) {
   134  	b, err := opts.ToAgentScheduleDHCPNetworkMap()
   135  	if err != nil {
   136  		r.Err = err
   137  		return
   138  	}
   139  	resp, err := c.Post(scheduleDHCPNetworkURL(c, id), b, nil, &gophercloud.RequestOpts{
   140  		OkCodes: []int{201},
   141  	})
   142  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   143  	return
   144  }
   145  
   146  // RemoveDHCPNetwork removes a network from a DHCP agent.
   147  func RemoveDHCPNetwork(c *gophercloud.ServiceClient, id string, networkID string) (r RemoveDHCPNetworkResult) {
   148  	resp, err := c.Delete(removeDHCPNetworkURL(c, id, networkID), nil)
   149  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   150  	return
   151  }
   152  
   153  // ListBGPSpeakers list the BGP Speakers hosted by a specific dragent
   154  // GET /v2.0/agents/{agent-id}/bgp-drinstances
   155  func ListBGPSpeakers(c *gophercloud.ServiceClient, agentID string) pagination.Pager {
   156  	url := listBGPSpeakersURL(c, agentID)
   157  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   158  		return ListBGPSpeakersResult{pagination.SinglePageBase(r)}
   159  	})
   160  }
   161  
   162  // ScheduleBGPSpeakerOptsBuilder declare a function that build ScheduleBGPSpeakerOpts into a request body
   163  type ScheduleBGPSpeakerOptsBuilder interface {
   164  	ToAgentScheduleBGPSpeakerMap() (map[string]interface{}, error)
   165  }
   166  
   167  // ScheduleBGPSpeakerOpts represents the data that would be POST to the endpoint
   168  type ScheduleBGPSpeakerOpts struct {
   169  	SpeakerID string `json:"bgp_speaker_id" required:"true"`
   170  }
   171  
   172  // ToAgentScheduleBGPSpeakerMap builds a request body from ScheduleBGPSpeakerOpts
   173  func (opts ScheduleBGPSpeakerOpts) ToAgentScheduleBGPSpeakerMap() (map[string]interface{}, error) {
   174  	return gophercloud.BuildRequestBody(opts, "")
   175  }
   176  
   177  // ScheduleBGPSpeaker schedule a BGP speaker to a BGP agent
   178  // POST /v2.0/agents/{agent-id}/bgp-drinstances
   179  func ScheduleBGPSpeaker(c *gophercloud.ServiceClient, agentID string, opts ScheduleBGPSpeakerOptsBuilder) (r ScheduleBGPSpeakerResult) {
   180  	b, err := opts.ToAgentScheduleBGPSpeakerMap()
   181  	if err != nil {
   182  		r.Err = err
   183  		return
   184  	}
   185  	resp, err := c.Post(scheduleBGPSpeakersURL(c, agentID), b, nil, &gophercloud.RequestOpts{
   186  		OkCodes: []int{201},
   187  	})
   188  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   189  	return
   190  }
   191  
   192  // RemoveBGPSpeaker removes a BGP speaker from a BGP agent
   193  // DELETE /v2.0/agents/{agent-id}/bgp-drinstances
   194  func RemoveBGPSpeaker(c *gophercloud.ServiceClient, agentID string, speakerID string) (r RemoveBGPSpeakerResult) {
   195  	resp, err := c.Delete(removeBGPSpeakersURL(c, agentID, speakerID), nil)
   196  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   197  	return
   198  }
   199  
   200  // ListDRAgentHostingBGPSpeakers the dragents that are hosting a specific bgp speaker
   201  // GET /v2.0/bgp-speakers/{bgp-speaker-id}/bgp-dragents
   202  func ListDRAgentHostingBGPSpeakers(c *gophercloud.ServiceClient, bgpSpeakerID string) pagination.Pager {
   203  	url := listDRAgentHostingBGPSpeakersURL(c, bgpSpeakerID)
   204  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   205  		return AgentPage{pagination.LinkedPageBase{PageResult: r}}
   206  	})
   207  }
   208  
   209  // ListL3Routers returns a list of routers scheduled to a specific
   210  // L3 agent.
   211  func ListL3Routers(c *gophercloud.ServiceClient, id string) (r ListL3RoutersResult) {
   212  	resp, err := c.Get(listL3RoutersURL(c, id), &r.Body, nil)
   213  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   214  	return
   215  }
   216  
   217  // ScheduleL3RouterOptsBuilder allows extensions to add additional parameters
   218  // to the ScheduleL3Router request.
   219  type ScheduleL3RouterOptsBuilder interface {
   220  	ToAgentScheduleL3RouterMap() (map[string]interface{}, error)
   221  }
   222  
   223  // ScheduleL3RouterOpts represents the attributes used when scheduling a
   224  // router to a L3 agent.
   225  type ScheduleL3RouterOpts struct {
   226  	RouterID string `json:"router_id" required:"true"`
   227  }
   228  
   229  // ToAgentScheduleL3RouterMap builds a request body from ScheduleL3RouterOpts.
   230  func (opts ScheduleL3RouterOpts) ToAgentScheduleL3RouterMap() (map[string]interface{}, error) {
   231  	return gophercloud.BuildRequestBody(opts, "")
   232  }
   233  
   234  // ScheduleL3Router schedule a router to a L3 agent.
   235  func ScheduleL3Router(c *gophercloud.ServiceClient, id string, opts ScheduleL3RouterOptsBuilder) (r ScheduleL3RouterResult) {
   236  	b, err := opts.ToAgentScheduleL3RouterMap()
   237  	if err != nil {
   238  		r.Err = err
   239  		return
   240  	}
   241  	resp, err := c.Post(scheduleL3RouterURL(c, id), b, nil, &gophercloud.RequestOpts{
   242  		OkCodes: []int{201},
   243  	})
   244  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   245  	return
   246  }
   247  
   248  // RemoveL3Router removes a router from a L3 agent.
   249  func RemoveL3Router(c *gophercloud.ServiceClient, id string, routerID string) (r RemoveL3RouterResult) {
   250  	resp, err := c.Delete(removeL3RouterURL(c, id, routerID), nil)
   251  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   252  	return
   253  }