github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/agents/requests.go (about)

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