Ejemplo n.º 1
0
// Update changes an existing endpoint with new data.
// All fields are optional in the provided EndpointOpts.
func Update(client *gophercloud.ServiceClient, endpointID string, opts EndpointOpts) UpdateResult {
	type endpoint struct {
		Interface *string `json:"interface,omitempty"`
		Name      *string `json:"name,omitempty"`
		Region    *string `json:"region,omitempty"`
		URL       *string `json:"url,omitempty"`
		ServiceID *string `json:"service_id,omitempty"`
	}

	type request struct {
		Endpoint endpoint `json:"endpoint"`
	}

	reqBody := request{Endpoint: endpoint{}}
	reqBody.Endpoint.Interface = gophercloud.MaybeString(string(opts.Availability))
	reqBody.Endpoint.Name = gophercloud.MaybeString(opts.Name)
	reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)
	reqBody.Endpoint.URL = gophercloud.MaybeString(opts.URL)
	reqBody.Endpoint.ServiceID = gophercloud.MaybeString(opts.ServiceID)

	var result UpdateResult
	_, result.Err = client.Request("PATCH", endpointURL(client, endpointID), gophercloud.RequestOpts{
		JSONBody:     &reqBody,
		JSONResponse: &result.Body,
		OkCodes:      []int{200},
	})
	return result
}
Ejemplo n.º 2
0
// Create accepts a CreateOpts struct and uses the values to create a new
// logical router. When it is created, the router does not have an internal
// interface - it is not associated to any subnet.
//
// You can optionally specify an external gateway for a router using the
// GatewayInfo struct. The external gateway for the router must be plugged into
// an external network (it is external if its `router:external' field is set to
// true).
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	type router struct {
		Name         *string      `json:"name,omitempty"`
		AdminStateUp *bool        `json:"admin_state_up,omitempty"`
		TenantID     *string      `json:"tenant_id,omitempty"`
		GatewayInfo  *GatewayInfo `json:"external_gateway_info,omitempty"`
	}

	type request struct {
		Router router `json:"router"`
	}

	reqBody := request{Router: router{
		Name:         gophercloud.MaybeString(opts.Name),
		AdminStateUp: opts.AdminStateUp,
		TenantID:     gophercloud.MaybeString(opts.TenantID),
	}}

	if opts.GatewayInfo != nil {
		reqBody.Router.GatewayInfo = opts.GatewayInfo
	}

	var res CreateResult
	_, res.Err = perigee.Request("POST", rootURL(c), perigee.Options{
		MoreHeaders: c.AuthenticatedHeaders(),
		ReqBody:     &reqBody,
		Results:     &res.Body,
		OkCodes:     []int{201},
	})
	return res
}
Ejemplo n.º 3
0
// Create accepts a CreateOpts struct and uses the values to create a new
// logical router. When it is created, the router does not have an internal
// interface - it is not associated to any subnet.
//
// You can optionally specify an external gateway for a router using the
// GatewayInfo struct. The external gateway for the router must be plugged into
// an external network (it is external if its `router:external' field is set to
// true).
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	type router struct {
		Name         *string      `json:"name,omitempty"`
		AdminStateUp *bool        `json:"admin_state_up,omitempty"`
		TenantID     *string      `json:"tenant_id,omitempty"`
		GatewayInfo  *GatewayInfo `json:"external_gateway_info,omitempty"`
	}

	type request struct {
		Router router `json:"router"`
	}

	reqBody := request{Router: router{
		Name:         gophercloud.MaybeString(opts.Name),
		AdminStateUp: opts.AdminStateUp,
		TenantID:     gophercloud.MaybeString(opts.TenantID),
	}}

	if opts.GatewayInfo != nil {
		reqBody.Router.GatewayInfo = opts.GatewayInfo
	}

	var res CreateResult
	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}
// ToRouterCreateMap casts a routerCreateOpts struct to a map.
func (opts RouterCreateOpts) ToRouterCreateMap() (map[string]interface{}, error) {
	r := make(map[string]interface{})

	if gophercloud.MaybeString(opts.Name) != nil {
		r["name"] = opts.Name
	}

	if opts.AdminStateUp != nil {
		r["admin_state_up"] = opts.AdminStateUp
	}

	if opts.Distributed != nil {
		r["distributed"] = opts.Distributed
	}

	if gophercloud.MaybeString(opts.TenantID) != nil {
		r["tenant_id"] = opts.TenantID
	}

	if opts.GatewayInfo != nil {
		r["external_gateway_info"] = opts.GatewayInfo
	}

	if opts.ValueSpecs != nil {
		for k, v := range opts.ValueSpecs {
			r[k] = v
		}
	}

	return map[string]interface{}{"router": r}, nil
}
Ejemplo n.º 5
0
// Update allows routers to be updated. You can update the name, administrative
// state, and the external gateway. For more information about how to set the
// external gateway for a router, see Create. This operation does not enable
// the update of router interfaces. To do this, use the AddInterface and
// RemoveInterface functions.
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
	type router struct {
		Name         *string      `json:"name,omitempty"`
		AdminStateUp *bool        `json:"admin_state_up,omitempty"`
		GatewayInfo  *GatewayInfo `json:"external_gateway_info,omitempty"`
	}

	type request struct {
		Router router `json:"router"`
	}

	reqBody := request{Router: router{
		Name:         gophercloud.MaybeString(opts.Name),
		AdminStateUp: opts.AdminStateUp,
	}}

	if opts.GatewayInfo != nil {
		reqBody.Router.GatewayInfo = opts.GatewayInfo
	}

	// Send request to API
	var res UpdateResult
	_, res.Err = perigee.Request("PUT", resourceURL(c, id), perigee.Options{
		MoreHeaders: c.AuthenticatedHeaders(),
		ReqBody:     &reqBody,
		Results:     &res.Body,
		OkCodes:     []int{200},
	})

	return res
}
Ejemplo n.º 6
0
// Update allows routers to be updated. You can update the name, administrative
// state, and the external gateway. For more information about how to set the
// external gateway for a router, see Create. This operation does not enable
// the update of router interfaces. To do this, use the AddInterface and
// RemoveInterface functions.
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
	type router struct {
		Name         *string      `json:"name,omitempty"`
		AdminStateUp *bool        `json:"admin_state_up,omitempty"`
		GatewayInfo  *GatewayInfo `json:"external_gateway_info,omitempty"`
		Routes       []Route      `json:"routes"`
	}

	type request struct {
		Router router `json:"router"`
	}

	reqBody := request{Router: router{
		Name:         gophercloud.MaybeString(opts.Name),
		AdminStateUp: opts.AdminStateUp,
	}}

	if opts.GatewayInfo != nil {
		reqBody.Router.GatewayInfo = opts.GatewayInfo
	}

	if opts.Routes != nil {
		reqBody.Router.Routes = opts.Routes
	}

	// Send request to API
	var res UpdateResult
	_, res.Err = c.Put(resourceURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})

	return res
}
Ejemplo n.º 7
0
// Update is an operation which modifies the attributes of the specified VIP.
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
	type vip struct {
		Name         string              `json:"name,omitempty"`
		PoolID       string              `json:"pool_id,omitempty"`
		Description  *string             `json:"description,omitempty"`
		Persistence  *SessionPersistence `json:"session_persistence,omitempty"`
		ConnLimit    *int                `json:"connection_limit,omitempty"`
		AdminStateUp *bool               `json:"admin_state_up,omitempty"`
	}

	type request struct {
		VirtualIP vip `json:"vip"`
	}

	reqBody := request{VirtualIP: vip{
		Name:         opts.Name,
		PoolID:       opts.PoolID,
		Description:  gophercloud.MaybeString(opts.Description),
		ConnLimit:    opts.ConnLimit,
		AdminStateUp: opts.AdminStateUp,
	}}

	if opts.Persistence != nil {
		reqBody.VirtualIP.Persistence = opts.Persistence
	}

	var res UpdateResult
	_, res.Err = c.Put(resourceURL(c, id), reqBody, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200, 202},
	})

	return res
}
Ejemplo n.º 8
0
// Update is an operation which modifies the attributes of the specified monitor.
func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) UpdateResult {
	var res UpdateResult

	if opts.Delay > 0 && opts.Timeout > 0 && opts.Delay < opts.Timeout {
		res.Err = errDelayMustGETimeout
	}

	type monitor struct {
		Delay         int     `json:"delay"`
		Timeout       int     `json:"timeout"`
		MaxRetries    int     `json:"max_retries"`
		URLPath       *string `json:"url_path,omitempty"`
		ExpectedCodes *string `json:"expected_codes,omitempty"`
		HTTPMethod    *string `json:"http_method,omitempty"`
		AdminStateUp  *bool   `json:"admin_state_up,omitempty"`
	}

	type request struct {
		Monitor monitor `json:"health_monitor"`
	}

	reqBody := request{Monitor: monitor{
		Delay:         opts.Delay,
		Timeout:       opts.Timeout,
		MaxRetries:    opts.MaxRetries,
		URLPath:       gophercloud.MaybeString(opts.URLPath),
		ExpectedCodes: gophercloud.MaybeString(opts.ExpectedCodes),
		HTTPMethod:    gophercloud.MaybeString(opts.HTTPMethod),
		AdminStateUp:  opts.AdminStateUp,
	}}

	_, res.Err = perigee.Request("PUT", resourceURL(c, id), perigee.Options{
		MoreHeaders: c.AuthenticatedHeaders(),
		ReqBody:     &reqBody,
		Results:     &res.Body,
		OkCodes:     []int{200, 202},
	})

	return res
}
Ejemplo n.º 9
0
// Create inserts a new Endpoint into the service catalog.
// Within EndpointOpts, Region may be omitted by being left as "", but all other fields are required.
func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
	// Redefined so that Region can be re-typed as a *string, which can be omitted from the JSON output.
	type endpoint struct {
		Interface string  `json:"interface"`
		Name      string  `json:"name"`
		Region    *string `json:"region,omitempty"`
		URL       string  `json:"url"`
		ServiceID string  `json:"service_id"`
	}

	type request struct {
		Endpoint endpoint `json:"endpoint"`
	}

	// Ensure that EndpointOpts is fully populated.
	if opts.Availability == "" {
		return createErr(ErrAvailabilityRequired)
	}
	if opts.Name == "" {
		return createErr(ErrNameRequired)
	}
	if opts.URL == "" {
		return createErr(ErrURLRequired)
	}
	if opts.ServiceID == "" {
		return createErr(ErrServiceIDRequired)
	}

	// Populate the request body.
	reqBody := request{
		Endpoint: endpoint{
			Interface: string(opts.Availability),
			Name:      opts.Name,
			URL:       opts.URL,
			ServiceID: opts.ServiceID,
		},
	}
	reqBody.Endpoint.Region = gophercloud.MaybeString(opts.Region)

	var result CreateResult
	_, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
		MoreHeaders: client.AuthenticatedHeaders(),
		ReqBody:     &reqBody,
		Results:     &result.Body,
		OkCodes:     []int{201},
	})
	return result
}
Ejemplo n.º 10
0
// Create is an operation which provisions a new health monitor. There are
// different types of monitor you can provision: PING, TCP or HTTP(S). Below
// are examples of how to create each one.
//
// Here is an example config struct to use when creating a PING or TCP monitor:
//
// CreateOpts{Type: TypePING, Delay: 20, Timeout: 10, MaxRetries: 3}
// CreateOpts{Type: TypeTCP, Delay: 20, Timeout: 10, MaxRetries: 3}
//
// Here is an example config struct to use when creating a HTTP(S) monitor:
//
// CreateOpts{Type: TypeHTTP, Delay: 20, Timeout: 10, MaxRetries: 3,
//  HttpMethod: "HEAD", ExpectedCodes: "200"}
//
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	var res CreateResult

	// Validate inputs
	allowed := map[string]bool{TypeHTTP: true, TypeHTTPS: true, TypeTCP: true, TypePING: true}
	if opts.Type == "" || allowed[opts.Type] == false {
		res.Err = errValidTypeRequired
	}
	if opts.Delay == 0 {
		res.Err = errDelayRequired
	}
	if opts.Timeout == 0 {
		res.Err = errTimeoutRequired
	}
	if opts.MaxRetries == 0 {
		res.Err = errMaxRetriesRequired
	}
	if opts.Type == TypeHTTP || opts.Type == TypeHTTPS {
		if opts.URLPath == "" {
			res.Err = errURLPathRequired
		}
		if opts.ExpectedCodes == "" {
			res.Err = errExpectedCodesRequired
		}
	}
	if opts.Delay < opts.Timeout {
		res.Err = errDelayMustGETimeout
	}
	if res.Err != nil {
		return res
	}

	type monitor struct {
		Type          string  `json:"type"`
		Delay         int     `json:"delay"`
		Timeout       int     `json:"timeout"`
		MaxRetries    int     `json:"max_retries"`
		TenantID      *string `json:"tenant_id,omitempty"`
		URLPath       *string `json:"url_path,omitempty"`
		ExpectedCodes *string `json:"expected_codes,omitempty"`
		HTTPMethod    *string `json:"http_method,omitempty"`
		AdminStateUp  *bool   `json:"admin_state_up,omitempty"`
	}

	type request struct {
		Monitor monitor `json:"health_monitor"`
	}

	reqBody := request{Monitor: monitor{
		Type:          opts.Type,
		Delay:         opts.Delay,
		Timeout:       opts.Timeout,
		MaxRetries:    opts.MaxRetries,
		TenantID:      gophercloud.MaybeString(opts.TenantID),
		URLPath:       gophercloud.MaybeString(opts.URLPath),
		ExpectedCodes: gophercloud.MaybeString(opts.ExpectedCodes),
		HTTPMethod:    gophercloud.MaybeString(opts.HTTPMethod),
		AdminStateUp:  opts.AdminStateUp,
	}}

	_, res.Err = c.Request("POST", rootURL(c), gophercloud.RequestOpts{
		JSONBody:     &reqBody,
		JSONResponse: &res.Body,
		OkCodes:      []int{201},
	})

	return res
}
Ejemplo n.º 11
0
// Create is an operation which provisions a new virtual IP based on the
// configuration defined in the CreateOpts struct. Once the request is
// validated and progress has started on the provisioning process, a
// CreateResult will be returned.
//
// Please note that the PoolID should refer to a pool that is not already
// associated with another vip. If the pool is already used by another vip,
// then the operation will fail with a 409 Conflict error will be returned.
//
// Users with an admin role can create VIPs on behalf of other tenants by
// specifying a TenantID attribute different than their own.
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	var res CreateResult

	// Validate required opts
	if opts.Name == "" {
		res.Err = errNameRequired
		return res
	}
	if opts.SubnetID == "" {
		res.Err = errSubnetIDRequried
		return res
	}
	if opts.Protocol == "" {
		res.Err = errProtocolRequired
		return res
	}
	if opts.ProtocolPort == 0 {
		res.Err = errProtocolPortRequired
		return res
	}
	if opts.PoolID == "" {
		res.Err = errPoolIDRequired
		return res
	}

	type vip struct {
		Name         string              `json:"name"`
		SubnetID     string              `json:"subnet_id"`
		Protocol     string              `json:"protocol"`
		ProtocolPort int                 `json:"protocol_port"`
		PoolID       string              `json:"pool_id"`
		Description  *string             `json:"description,omitempty"`
		TenantID     *string             `json:"tenant_id,omitempty"`
		Address      *string             `json:"address,omitempty"`
		Persistence  *SessionPersistence `json:"session_persistence,omitempty"`
		ConnLimit    *int                `json:"connection_limit,omitempty"`
		AdminStateUp *bool               `json:"admin_state_up,omitempty"`
	}

	type request struct {
		VirtualIP vip `json:"vip"`
	}

	reqBody := request{VirtualIP: vip{
		Name:         opts.Name,
		SubnetID:     opts.SubnetID,
		Protocol:     opts.Protocol,
		ProtocolPort: opts.ProtocolPort,
		PoolID:       opts.PoolID,
		Description:  gophercloud.MaybeString(opts.Description),
		TenantID:     gophercloud.MaybeString(opts.TenantID),
		Address:      gophercloud.MaybeString(opts.Address),
		ConnLimit:    opts.ConnLimit,
		AdminStateUp: opts.AdminStateUp,
	}}

	if opts.Persistence != nil {
		reqBody.VirtualIP.Persistence = opts.Persistence
	}

	_, res.Err = perigee.Request("POST", rootURL(c), perigee.Options{
		MoreHeaders: c.AuthenticatedHeaders(),
		ReqBody:     &reqBody,
		Results:     &res.Body,
		OkCodes:     []int{201},
	})

	return res
}