Ejemplo n.º 1
0
// Create member for specific image
//
// Preconditions
//    The specified images must exist.
//    You can only add a new member to an image which 'visibility' attribute is private.
//    You must be the owner of the specified image.
// Synchronous Postconditions
//    With correct permissions, you can see the member status of the image as pending through API calls.
//
// More details here: http://developer.openstack.org/api-ref-image-v2.html#createImageMember-v2
func Create(client *gophercloud.ServiceClient, id string, member string) CreateMemberResult {
	var res CreateMemberResult
	body := map[string]interface{}{}
	body["member"] = member

	response, err := client.Post(imageMembersURL(client, id), body, &res.Body,
		&gophercloud.RequestOpts{OkCodes: []int{200, 409, 403}})

	//some problems in http stack or lower
	if err != nil {
		res.Err = err
		return res
	}

	// membership conflict
	if response.StatusCode == 409 {
		res.Err = fmt.Errorf("Given tenant '%s' is already member for image '%s'.", member, id)
		return res
	}

	// visibility conflict
	if response.StatusCode == 403 {
		res.Err = fmt.Errorf("You can only add a new member to an image "+
			"which 'visibility' attribute is private (image '%s')", id)
		return res
	}

	return res
}
Ejemplo n.º 2
0
// Create accepts a CreateOpts struct and uses the values provided to create a
// new floating IP resource. You can create floating IPs on external networks
// only. If you provide a FloatingNetworkID which refers to a network that is
// not external (i.e. its `router:external' attribute is False), the operation
// will fail and return a 400 error.
//
// If you do not specify a FloatingIP address value, the operation will
// automatically allocate an available address for the new resource. If you do
// choose to specify one, it must fall within the subnet range for the external
// network - otherwise the operation returns a 400 error. If the FloatingIP
// address is already in use, the operation returns a 409 error code.
//
// You can associate the new resource with an internal port by using the PortID
// field. If you specify a PortID that is not valid, the operation will fail and
// return 404 error code.
//
// You must also configure an IP address for the port associated with the PortID
// you have provided - this is what the FixedIP refers to: an IP fixed to a port.
// Because a port might be associated with multiple IP addresses, you can use
// the FixedIP field to associate a particular IP address rather than have the
// API assume for you. If you specify an IP address that is not valid, the
// operation will fail and return a 400 error code. If the PortID and FixedIP
// are already associated with another resource, the operation will fail and
// returns a 409 error code.
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	var res CreateResult

	// Validate
	if opts.FloatingNetworkID == "" {
		res.Err = errFloatingNetworkIDRequired
		return res
	}

	// Define structures
	type floatingIP struct {
		FloatingNetworkID string `json:"floating_network_id"`
		FloatingIP        string `json:"floating_ip_address,omitempty"`
		PortID            string `json:"port_id,omitempty"`
		FixedIP           string `json:"fixed_ip_address,omitempty"`
		TenantID          string `json:"tenant_id,omitempty"`
	}
	type request struct {
		FloatingIP floatingIP `json:"floatingip"`
	}

	// Populate request body
	reqBody := request{FloatingIP: floatingIP{
		FloatingNetworkID: opts.FloatingNetworkID,
		FloatingIP:        opts.FloatingIP,
		PortID:            opts.PortID,
		FixedIP:           opts.FixedIP,
		TenantID:          opts.TenantID,
	}}

	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 3
0
// Create accepts a CreateOpts struct and uses the values to create a new
// load balancer pool.
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	type pool struct {
		Name     string `json:"name"`
		TenantID string `json:"tenant_id,omitempty"`
		Protocol string `json:"protocol"`
		SubnetID string `json:"subnet_id"`
		LBMethod string `json:"lb_method"`
		Provider string `json:"provider,omitempty"`
	}
	type request struct {
		Pool pool `json:"pool"`
	}

	reqBody := request{Pool: pool{
		Name:     opts.Name,
		TenantID: opts.TenantID,
		Protocol: opts.Protocol,
		SubnetID: opts.SubnetID,
		LBMethod: opts.LBMethod,
		Provider: opts.Provider,
	}}

	var res CreateResult
	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 4
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
}
Ejemplo n.º 5
0
// Create is an operation which provisions a new security group with default
// security group rules for the IPv4 and IPv6 ether types.
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	var res CreateResult

	// Validate required opts
	if opts.Name == "" {
		res.Err = errNameRequired
		return res
	}

	type secgroup struct {
		Name        string `json:"name"`
		TenantID    string `json:"tenant_id,omitempty"`
		Description string `json:"description,omitempty"`
	}

	type request struct {
		SecGroup secgroup `json:"security_group"`
	}

	reqBody := request{SecGroup: secgroup{
		Name:        opts.Name,
		TenantID:    opts.TenantID,
		Description: opts.Description,
	}}

	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 6
0
// Migrate is the admin operation to migrate a Compute Server.
func Migrate(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
	var req struct {
		Migrate string `json:"migrate"`
	}

	var res gophercloud.ErrResult
	_, res.Err = client.Post(actionURL(client, id), req, nil, nil)
	return res
}
Ejemplo n.º 7
0
// InjectNetworkInfo is the admin operation which injects network info into a Compute Server.
func InjectNetworkInfo(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
	var req struct {
		InjectNetworkInfo string `json:"injectNetworkInfo"`
	}

	var res gophercloud.ErrResult
	_, res.Err = client.Post(actionURL(client, id), req, nil, nil)
	return res
}
Ejemplo n.º 8
0
// ResetNetwork is the admin operation to reset the network on a Compute Server.
func ResetNetwork(client *gophercloud.ServiceClient, id string) gophercloud.ErrResult {
	var req struct {
		ResetNetwork string `json:"resetNetwork"`
	}

	var res gophercloud.ErrResult
	_, res.Err = client.Post(actionURL(client, id), req, nil, nil)
	return res
}
Ejemplo n.º 9
0
// ConfirmResize confirms a previous resize operation on a server.
// See Resize() for more details.
func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
	var res ActionResult

	reqBody := map[string]interface{}{"confirmResize": nil}
	_, res.Err = client.Post(actionURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{201, 202, 204},
	})
	return res
}
Ejemplo n.º 10
0
// Disassociate decouples an allocated floating IP from an instance
func Disassociate(client *gophercloud.ServiceClient, serverId, fip string) DisassociateResult {
	var res DisassociateResult

	removeFloatingIp := make(map[string]interface{})
	removeFloatingIp["address"] = fip
	reqBody := map[string]interface{}{"removeFloatingIp": removeFloatingIp}

	_, res.Err = client.Post(disassociateURL(client, serverId), reqBody, nil, nil)
	return res
}
Ejemplo n.º 11
0
// Execute requests the given policy be executed immediately.
func Execute(client *gophercloud.ServiceClient, groupID, policyID string) ExecuteResult {
	var result ExecuteResult

	url := executeURL(client, groupID, policyID)
	_, result.Err = client.Post(url, nil, &result.Body, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})

	return result
}
Ejemplo n.º 12
0
// Create implements create image request
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
	var res CreateResult
	body, err := opts.ToImageCreateMap()
	if err != nil {
		res.Err = err
		return res
	}
	_, res.Err = client.Post(createURL(client), body, &res.Body, &gophercloud.RequestOpts{OkCodes: []int{201}})
	return res
}
Ejemplo n.º 13
0
func ForceDelete(client *gophercloud.ServiceClient, id string) ActionResult {
	var req struct {
		ForceDelete string `json:"forceDelete"`
	}

	var res ActionResult
	_, res.Err = client.Post(actionURL(client, id), req, nil, nil)
	return res

}
Ejemplo n.º 14
0
//OpenstackPost posts a resource using OpenStack API
func OpenstackPost(client *gophercloud.ServiceClient, url string, data interface{}) (interface{}, error) {
	var response interface{}
	_, err := client.Post(url, data, &response, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})
	if err != nil {
		return nil, err
	}
	return response, nil
}
Ejemplo n.º 15
0
// Create adds a public IP to the server with the given serverID.
func Create(c *gophercloud.ServiceClient, serverID string) CreateResult {
	var res CreateResult
	reqBody := map[string]interface{}{
		"cloud_server": map[string]string{
			"id": serverID,
		},
	}
	_, res.Err = c.Post(createURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 16
0
// Create adds a new service of the requested type to the catalog.
func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult {
	type request struct {
		Type string `json:"type"`
	}

	req := request{Type: serviceType}

	var result CreateResult
	_, result.Err = client.Post(listURL(client), req, &result.Body, nil)
	return result
}
Ejemplo n.º 17
0
// Create accepts a CreateOpts struct and creates a new project using the values
// provided.
//
// The tenant ID that is contained in the URI is the tenant that creates the
// network. An admin user, however, has the option of specifying another tenant
// ID in the CreateOpts struct.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
	var res CreateResult

	reqBody, err := opts.ToProjectCreateMap()
	if err != nil {
		res.Err = err
		return res
	}
	_, res.Err = client.Post(createURL(client), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 18
0
// Resize instructs the provider to change the flavor of the server.
// Note that this implies rebuilding it.
// Unfortunately, one cannot pass rebuild parameters to the resize function.
// When the resize completes, the server will be in RESIZE_VERIFY state.
// While in this state, you can explore the use of the new server's configuration.
// If you like it, call ConfirmResize() to commit the resize permanently.
// Otherwise, call RevertResize() to restore the old configuration.
func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
	var res ActionResult
	reqBody, err := opts.ToServerResizeMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
	return res
}
Ejemplo n.º 19
0
// CreateNodes adds the cloud servers with the given serverIDs to the corresponding
// load balancer pools with the given poolIDs.
func CreateNodes(c *gophercloud.ServiceClient, opts NodesOpts) CreateNodesResult {
	var res CreateNodesResult
	reqBody, err := opts.ToLBPoolCreateNodesMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = c.Post(createNodesURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 20
0
// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
// This operation does not affect already-existing metadata that is not specified
// by opts.
func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
	var res UpdateMetadataResult
	metadata, err := opts.ToMetadataUpdateMap()
	if err != nil {
		res.Err = err
		return res
	}
	_, res.Err = client.Post(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
	return res
}
Ejemplo n.º 21
0
// Create authenticates to the identity service and attempts to acquire a Token.
// If successful, the CreateResult
// Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(),
// which abstracts all of the gory details about navigating service catalogs and such.
func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateResult {
	request, err := auth.ToTokenCreateMap()
	if err != nil {
		return CreateResult{gophercloud.Result{Err: err}}
	}

	var result CreateResult
	_, result.Err = client.Post(CreateURL(client), request, &result.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200, 203},
	})
	return result
}
Ejemplo n.º 22
0
// ResetNetwork is the admin operation to create a backup of a Compute Server.
func CreateBackup(client *gophercloud.ServiceClient, id string, opts CreateBackupOpts) gophercloud.ErrResult {
	var res gophercloud.ErrResult

	req, err := opts.ToCreateBackupMap()
	if err != nil {
		res.Err = err
		return res
	}
	_, res.Err = client.Post(actionURL(client, id), req, nil, nil)
	return res

}
Ejemplo n.º 23
0
// Adopt accepts an AdoptOpts struct and creates a new stack using the resources
// from another stack.
func Adopt(c *gophercloud.ServiceClient, opts AdoptOptsBuilder) AdoptResult {
	var res AdoptResult

	reqBody, err := opts.ToStackAdoptMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = c.Post(adoptURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 24
0
// Create is the operation responsible for adding network items to the access
// rules for a particular load balancer. If network items already exist, the
// new item will be appended. A single IP address or subnet range is considered
// unique and cannot be duplicated.
func Create(client *gophercloud.ServiceClient, loadBalancerID int, opts CreateOptsBuilder) CreateResult {
	var res CreateResult

	reqBody, err := opts.ToAccessListCreateMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = client.Post(rootURL(client, loadBalancerID), reqBody, nil, nil)
	return res
}
Ejemplo n.º 25
0
// Attach will detach a volume based on volume id.
func Detach(client *gophercloud.ServiceClient, id string) DetachResult {
	var res DetachResult

	v := make(map[string]interface{})
	reqBody := map[string]interface{}{"os-detach": v}

	_, res.Err = client.Post(detachURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{202},
	})

	return res
}
Ejemplo n.º 26
0
// ResetState is the admin operation to reset the state of a server.
func ResetState(client *gophercloud.ServiceClient, id string, state string) gophercloud.ErrResult {
	var res gophercloud.ErrResult
	var req struct {
		ResetState struct {
			State string `json:"state"`
		} `json:"os-resetState"`
	}
	req.ResetState.State = state

	_, res.Err = client.Post(actionURL(client, id), req, nil, nil)
	return res
}
Ejemplo n.º 27
0
// Reserve will reserve a volume based on volume id.
func Reserve(client *gophercloud.ServiceClient, id string) ReserveResult {
	var res ReserveResult

	v := make(map[string]interface{})
	reqBody := map[string]interface{}{"os-reserve": v}

	_, res.Err = client.Post(reserveURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
		OkCodes: []int{200, 201, 202},
	})

	return res
}
Ejemplo n.º 28
0
// Create is the operation responsible for asynchronously provisioning a new
// load balancer based on the configuration defined in CreateOpts. Once the
// request is validated and progress has started on the provisioning process, a
// response struct is returned. When extracted (with Extract()), you have
// to the load balancer's unique ID and status.
//
// Once an ID is attained, you can check on the progress of the operation by
// calling Get and passing in the ID. If the corresponding request cannot be
// fulfilled due to insufficient or invalid data, an HTTP 400 (Bad Request)
// error response is returned with information regarding the nature of the
// failure in the body of the response. Failures in the validation process are
// non-recoverable and require the caller to correct the cause of the failure.
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
	var res CreateResult

	reqBody, err := opts.ToLBCreateMap()
	if err != nil {
		res.Err = err
		return res
	}

	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 29
0
// Create is an operation which adds a new security group rule and associates it
// with an existing security group (whose ID is specified in CreateOpts).
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
	var res CreateResult

	// Validate required opts
	if opts.Direction != DirIngress && opts.Direction != DirEgress {
		res.Err = errValidDirectionRequired
		return res
	}
	if opts.EtherType != Ether4 && opts.EtherType != Ether6 {
		res.Err = errValidEtherTypeRequired
		return res
	}
	if opts.SecGroupID == "" {
		res.Err = errSecGroupIDRequired
		return res
	}
	if opts.Protocol != "" && opts.Protocol != ProtocolTCP && opts.Protocol != ProtocolUDP && opts.Protocol != ProtocolICMP {
		res.Err = errValidProtocolRequired
		return res
	}

	type secrule struct {
		Direction      string `json:"direction"`
		EtherType      string `json:"ethertype"`
		SecGroupID     string `json:"security_group_id"`
		PortRangeMax   int    `json:"port_range_max,omitempty"`
		PortRangeMin   int    `json:"port_range_min,omitempty"`
		Protocol       string `json:"protocol,omitempty"`
		RemoteGroupID  string `json:"remote_group_id,omitempty"`
		RemoteIPPrefix string `json:"remote_ip_prefix,omitempty"`
		TenantID       string `json:"tenant_id,omitempty"`
	}

	type request struct {
		SecRule secrule `json:"security_group_rule"`
	}

	reqBody := request{SecRule: secrule{
		Direction:      opts.Direction,
		EtherType:      opts.EtherType,
		SecGroupID:     opts.SecGroupID,
		PortRangeMax:   opts.PortRangeMax,
		PortRangeMin:   opts.PortRangeMin,
		Protocol:       opts.Protocol,
		RemoteGroupID:  opts.RemoteGroupID,
		RemoteIPPrefix: opts.RemoteIPPrefix,
		TenantID:       opts.TenantID,
	}}

	_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
	return res
}
Ejemplo n.º 30
0
// SendMetrics will ingest the pre aggregated timer metrics for the tenant associated with RackspaceMetrics.
func SendAggregatedTimers(c *gophercloud.ServiceClient, tenantId string, timestamp int64, Timers []Timer) {
	var res PostResult

	timers := convertTimers(Timers)
	reqBody := map[string]interface{}{
		"tenantId":  tenantId,
		"timestamp": timestamp,
		"timers":    timers,
	}
	_, res.Err = c.Post(getURLForIngestAggregatedMetrics(c), reqBody, &res.Body, &gophercloud.RequestOpts{
		OkCodes: []int{200},
	})
}