예제 #1
0
// papersPlease contains the common logic between authentication and re-authentication.
// The name, obviously a joke on the process of authentication, was chosen because
// of how many other entities exist in the program containing the word Auth or Authorization.
// I didn't need another one.
func (c *Context) papersPlease(p Provider, options AuthOptions) (*Access, error) {
	var access *Access

	if (options.Username == "") || (options.Password == "") {
		return nil, ErrCredentials
	}

	err := perigee.Post(p.AuthEndpoint, perigee.Options{
		CustomClient: c.httpClient,
		ReqBody: &AuthContainer{
			Auth: Auth{
				PasswordCredentials: PasswordCredentials{
					Username: options.Username,
					Password: options.Password,
				},
				TenantId:   options.TenantId,
				TenantName: options.TenantName,
			},
		},
		Results: &struct {
			Access **Access `json:"access"`
		}{
			&access,
		},
	})
	if err == nil {
		access.options = options
		access.provider = p
		access.context = c
	}
	return access, err
}
예제 #2
0
func (gsp *genericServersProvider) CreateFloatingIp(pool string) (FloatingIp, error) {
	fip := new(FloatingIp)

	err := gsp.context.WithReauth(gsp.access, func() error {
		url := gsp.endpoint + "/os-floating-ips"
		return perigee.Post(url, perigee.Options{
			CustomClient: gsp.context.httpClient,
			ReqBody: map[string]string{
				"pool": pool,
			},
			Results: &struct {
				FloatingIp **FloatingIp `json:"floating_ip"`
			}{&fip},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
		})
	})

	if fip.Ip == "" {
		return *fip, errors.New("Error creating floating IP")
	}

	return *fip, err
}
예제 #3
0
파일: identity.go 프로젝트: racker/gorax
// Authenticate attempts to verify this Identity object's credentials.
func (id *identity) Authenticate() error {
	creds := &AuthContainer{
		Auth: Auth{
			PasswordCredentials: PasswordCredentials{
				Username: id.username,
				Password: id.password,
			},
		},
	}

	err := perigee.Post(id.AuthEndpoint(), perigee.Options{
		CustomClient: id.httpClient,
		ReqBody:      creds,
		Results:      &id.access,
	})
	if err != nil {
		return err
	}

	id.isAuthenticated = true
	id.token = id.access.Access.Token.Id
	id.expires = id.access.Access.Token.Expires
	id.tenantId = id.access.Access.Token.Tenant.Id
	id.tenantName = id.access.Access.Token.Tenant.Name
	return nil
}
예제 #4
0
파일: region.go 프로젝트: jyidiego/gorax
// RevertResizeServer will reject a server's resized configuration, thus
// rolling back to the original server.
func (r *raxRegion) RevertResizeServer(id string) error {
	baseUrl, err := r.EndpointByName("servers")
	serverUrl := fmt.Sprintf("%s/%s/action", baseUrl, id)
	err = perigee.Post(serverUrl, perigee.Options{
		ReqBody: &struct {
			RevertResize *int `json:"revertResize"`
		}{nil},
		OkCodes: []int{204},
		MoreHeaders: map[string]string{
			"X-Auth-Token": r.token,
		},
	})
	return err
}
예제 #5
0
// See the CloudServersProvider interface for details
func (gsp *genericServersProvider) UnrescueServer(id string) error {
	return gsp.context.WithReauth(gsp.access, func() error {
		url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
		return perigee.Post(url, perigee.Options{
			ReqBody: &struct {
				Unrescue *int `json:"unrescue"`
			}{nil},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			OkCodes: []int{202},
		})
	})
}
func (gsp *genericServersProvider) AssociateFloatingIp(serverId string, ip FloatingIp) error {
	return gsp.context.WithReauth(gsp.access, func() error {
		ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, serverId)
		return perigee.Post(ep, perigee.Options{
			CustomClient: gsp.context.httpClient,
			ReqBody: map[string](map[string]string){
				"addFloatingIp": map[string]string{"address": ip.Ip},
			},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			OkCodes: []int{202},
		})
	})
}
예제 #7
0
// See the CloudServersProvider interface for details.
func (gsp *genericServersProvider) CreateDefaultSGRule(r SGRule) (*SGRule, error) {
	var sgr *SGRule
	err := gsp.context.WithReauth(gsp.access, func() error {
		ep := fmt.Sprintf("%s/os-security-group-default-rules", gsp.endpoint)
		return perigee.Post(ep, perigee.Options{
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			Results: &struct{ Security_group_default_rule **SGRule }{&sgr},
			ReqBody: struct {
				Security_group_default_rule SGRule `json:"security_group_default_rule"`
			}{r},
		})
	})
	return sgr, err
}
예제 #8
0
func (gnp *genericNetworksProvider) AssociateMonitor(monitorId string, poolId string) error {
	monitor := Monitor{Id: monitorId}

	ep := gnp.endpoint + "/v2.0/lb/pools/" + poolId + "/health_monitors"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			Monitor *Monitor `json:"health_monitor"`
		}{&monitor},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return err
}
예제 #9
0
func (gnp *genericNetworksProvider) CreateSubnet(ns NewSubnet) (*Subnet, error) {
	var s *Subnet

	ep := gnp.endpoint + "/v2.0/subnets"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewSubnet *NewSubnet `json:"subnet"`
		}{&ns},
		Results: &struct{ Subnet **Subnet }{&s},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return s, err
}
예제 #10
0
func (gnp *genericNetworksProvider) CreateVip(newVip NewVip) (*Vip, error) {
	var vip *Vip

	ep := gnp.endpoint + "/v2.0/lb/vips"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewVip *NewVip `json:"vip"`
		}{&newVip},
		Results: &struct{ Vip **Vip }{&vip},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return vip, err
}
예제 #11
0
func (gnp *genericNetworksProvider) CreateMember(newMember NewMember) (*Member, error) {
	var member *Member

	ep := gnp.endpoint + "/v2.0/lb/members"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewMember *NewMember `json:"member"`
		}{&newMember},
		Results: &struct{ Member **Member }{&member},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return member, err
}
예제 #12
0
func (gnp *genericNetworksProvider) CreatePool(newPool NewPool) (*Pool, error) {
	var pool *Pool

	ep := gnp.endpoint + "/v2.0/lb/pools"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewPool *NewPool `json:"pool"`
		}{&newPool},
		Results: &struct{ Pool **Pool }{&pool},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return pool, err
}
예제 #13
0
func (gnp *genericNetworksProvider) CreateNetwork(nn NewNetwork) (*Network, error) {
	var n *Network

	ep := gnp.endpoint + "/v2.0/networks"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewNetwork *NewNetwork `json:"network"`
		}{&nn},
		Results: &struct{ Network **Network }{&n},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return n, err
}
예제 #14
0
func (gnp *genericNetworksProvider) CreateSecurityGroupRule(nsgr SecurityGroupRule) (*SecurityGroupRule, error) {
	var sgr *SecurityGroupRule

	ep := gnp.endpoint + "/v2.0/security-group-rules"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			SecurityGroupRule *SecurityGroupRule `json:"security_group_rule"`
		}{&nsgr},
		Results: &struct{ SecurityGroupRule **SecurityGroupRule }{&sgr},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return sgr, err
}
예제 #15
0
func (gsp *genericServersProvider) CreateKeyPair(nkp NewKeyPair) (KeyPair, error) {
	var kp KeyPair

	err := gsp.context.WithReauth(gsp.access, func() error {
		url := gsp.endpoint + "/os-keypairs"
		return perigee.Post(url, perigee.Options{
			ReqBody: &struct {
				KeyPair *NewKeyPair `json:"keypair"`
			}{&nkp},
			CustomClient: gsp.context.httpClient,
			Results:      &struct{ KeyPair *KeyPair }{&kp},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
		})
	})
	return kp, err
}
예제 #16
0
파일: region.go 프로젝트: jyidiego/gorax
// RebuildServer removes all data on the server and replaces it with the specified image
// on the specified flavor.
func (r *raxRegion) RebuildServer(id string, ns NewServer) (*Server, error) {
	var s *Server
	baseUrl, err := r.EndpointByName("servers")
	serverUrl := fmt.Sprintf("%s/%s/action", baseUrl, id)
	err = perigee.Post(serverUrl, perigee.Options{
		ReqBody: &struct {
			Rebuild NewServer `json:"rebuild"`
		}{Rebuild: ns},
		Results: &struct {
			Server **Server `json:"server"`
		}{&s},
		OkCodes: []int{202},
		MoreHeaders: map[string]string{
			"X-Auth-Token": r.token,
		},
	})
	return s, err
}
예제 #17
0
func (gnp *genericNetworksProvider) CreateSecurityGroup(nsg NewSecurityGroup) (*SecurityGroup, error) {
	var csg *SecurityGroup
	ep := gnp.endpoint + "/v2.0/security-groups"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewSecurityGroup *NewSecurityGroup `json:"security_group"`
		}{&nsg},
		Results: &struct {
			SecurityGroup **SecurityGroup `json:"security_group"`
		}{&csg},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return csg, err
}
예제 #18
0
func (gnp *genericNetworksProvider) CreateFirewall(newFirewall NewFirewall) (*Firewall, error) {
	var firewall *Firewall

	ep := gnp.endpoint + "/v2.0/fw/firewalls"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewFirewall *NewFirewall `json:"firewall"`
		}{&newFirewall},
		Results: &struct {
			Firewall **Firewall `json:"firewall"`
		}{&firewall},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return firewall, err
}
예제 #19
0
파일: region.go 프로젝트: jyidiego/gorax
// ResizeServer can be a short-hand for RebuildServer where only the size of the server
// changes.  Note that after the resize operation is requested, you will need to confirm
// the resize has completed for changes to take effect permanently.  Changes will assume
// to be confirmed even without an explicit confirmation after 24 hours from the initial
// request.
func (r *raxRegion) ResizeServer(id, name, flavor, diskConfig string) error {
	baseUrl, err := r.EndpointByName("servers")
	serverUrl := fmt.Sprintf("%s/%s/action", baseUrl, id)
	rr := ResizeRequest{
		Name:       name,
		FlavorRef:  flavor,
		DiskConfig: diskConfig,
	}
	err = perigee.Post(serverUrl, perigee.Options{
		ReqBody: &struct {
			Resize ResizeRequest `json:"resize"`
		}{rr},
		OkCodes: []int{202},
		MoreHeaders: map[string]string{
			"X-Auth-Token": r.token,
		},
	})
	return err
}
예제 #20
0
// See the CloudServersProvider interface for details.
func (gsp *genericServersProvider) RebuildServer(id string, ns NewServer) (*Server, error) {
	var s *Server

	err := gsp.context.WithReauth(gsp.access, func() error {
		ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
		return perigee.Post(ep, perigee.Options{
			ReqBody: &struct {
				Rebuild *NewServer `json:"rebuild"`
			}{&ns},
			Results: &struct{ Server **Server }{&s},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			OkCodes: []int{202},
		})
	})

	return s, err
}
예제 #21
0
// See the CloudServersProvider interface for details.
func (gsp *genericServersProvider) CreateSecurityGroup(desired SecurityGroup) (*SecurityGroup, error) {
	var actual *SecurityGroup

	err := gsp.context.WithReauth(gsp.access, func() error {
		ep := fmt.Sprintf("%s/os-security-groups", gsp.endpoint)
		return perigee.Post(ep, perigee.Options{
			ReqBody: struct {
				AddSecurityGroup SecurityGroup `json:"security_group"`
			}{desired},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			Results: &struct {
				SecurityGroup **SecurityGroup `json:"security_group"`
			}{&actual},
		})
	})
	return actual, err
}
예제 #22
0
// See the CloudServersProvider interface for details
func (gsp *genericServersProvider) RescueServer(id string) (string, error) {
	var pw *string

	err := gsp.context.WithReauth(gsp.access, func() error {
		url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
		return perigee.Post(url, perigee.Options{
			ReqBody: &struct {
				Rescue string `json:"rescue"`
			}{"none"},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			Results: &struct {
				AdminPass **string `json:"adminPass"`
			}{&pw},
		})
	})
	return *pw, err
}
예제 #23
0
func (gnp *genericNetworksProvider) CreateFirewallRule(newPolicy NewFirewallRule) (*FirewallRule, error) {
	var policy *FirewallRule

	ep := gnp.endpoint + "/v2.0/fw/firewall_rules"
	err := perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			NewFirewallRule *NewFirewallRule `json:"firewall_rule"`
		}{&newPolicy},
		Results: &struct {
			FirewallRule **FirewallRule `json:"firewall_rule"`
		}{&policy},
		MoreHeaders: map[string]string{
			"X-Auth-Token": gnp.access.AuthToken(),
		},
		OkCodes: []int{201},
	})

	return policy, err
}
예제 #24
0
// See the CloudServersProvider interface for details.
func (gsp *genericServersProvider) CreateServer(ns NewServer) (*NewServer, error) {
	var s *NewServer

	err := gsp.context.WithReauth(gsp.access, func() error {
		ep := gsp.endpoint + "/servers"
		return perigee.Post(ep, perigee.Options{
			ReqBody: &struct {
				Server *NewServer `json:"server"`
			}{&ns},
			Results: &struct{ Server **NewServer }{&s},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
			OkCodes: []int{202},
		})
	})

	return s, err
}
예제 #25
0
파일: region.go 프로젝트: jyidiego/gorax
// CreateServer requests a new server to be created by the cloud provider.
// The user must pass in a pointer to an initialized NewServerContainer structure.
// Please refer to NewServerContainer for more details.
//
// If the NewServer structure lacks a field assignment for AdminPass,
// a password will be automatically generated by OpenStack / Rackspace and
// returned back through the AdminPass field.  Take care; this will be
// the only time this happens; no other means exists in the public API
// to acquire a password for a pre-existing server.
func (r *raxRegion) CreateServer(ns NewServer) (*NewServer, error) {
	var s *NewServer

	ep, err := r.EndpointByName("servers")
	if err != nil {
		return nil, err
	}
	err = perigee.Post(ep, perigee.Options{
		ReqBody: &struct {
			Server *NewServer `json:"server"`
		}{&ns},
		Results: &struct{ Server **NewServer }{&s},
		MoreHeaders: map[string]string{
			"X-Auth-Token": r.token,
		},
		OkCodes: []int{202},
	})
	return s, err
}
예제 #26
0
// See the CloudServersProvider interface for details.
func (gsp *genericServersProvider) ResizeServer(id, newName, newFlavor, newDiskConfig string) error {
	err := gsp.context.WithReauth(gsp.access, func() error {
		url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
		rr := ResizeRequest{
			Name:       newName,
			FlavorRef:  newFlavor,
			DiskConfig: newDiskConfig,
		}
		return perigee.Post(url, perigee.Options{
			ReqBody: &struct {
				Resize ResizeRequest `json:"resize"`
			}{rr},
			OkCodes: []int{202},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
		})
	})
	return err
}
예제 #27
0
// See the CloudServersProvider interface for details
func (gsp *genericServersProvider) RebootServer(id string, hard bool) error {
	return gsp.context.WithReauth(gsp.access, func() error {
		url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
		types := map[bool]string{false: "SOFT", true: "HARD"}
		return perigee.Post(url, perigee.Options{
			ReqBody: &struct {
				Reboot struct {
					Type string `json:"type"`
				} `json:"reboot"`
			}{
				struct {
					Type string `json:"type"`
				}{types[hard]},
			},
			OkCodes: []int{202},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
		})
	})
}
예제 #28
0
// See the CloudServersProvider interface for details.
func (gsp *genericServersProvider) SetAdminPassword(id, pw string) error {
	err := gsp.context.WithReauth(gsp.access, func() error {
		url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
		return perigee.Post(url, perigee.Options{
			ReqBody: &struct {
				ChangePassword struct {
					AdminPass string `json:"adminPass"`
				} `json:"changePassword"`
			}{
				struct {
					AdminPass string `json:"adminPass"`
				}{pw},
			},
			OkCodes: []int{202},
			MoreHeaders: map[string]string{
				"X-Auth-Token": gsp.access.AuthToken(),
			},
		})
	})
	return err
}
예제 #29
0
파일: region.go 프로젝트: jyidiego/gorax
// SetAdminPassword requests that the server with the specified ID
// have its administrative password changed.  For Linux, BSD, or other
// pure-POSIX-compliant systems, this administrator corresponds to the
// root user.  For Windows machines, the Administrator user will be
// affected.
func (r *raxRegion) SetAdminPassword(id string, pw string) error {
	ep, err := r.EndpointByName("servers")
	if err != nil {
		return err
	}
	endpoint := fmt.Sprintf("%s/%s/action", ep, id)
	err = perigee.Post(endpoint, perigee.Options{
		ReqBody: &struct {
			ChangePassword struct {
				AdminPass string `json:"adminPass"`
			} `json:"changePassword"`
		}{
			struct {
				AdminPass string `json:"adminPass"`
			}{pw},
		},
		OkCodes: []int{202},
		MoreHeaders: map[string]string{
			"X-Auth-Token": r.token,
		},
	})
	return err
}
예제 #30
0
파일: region.go 프로젝트: jyidiego/gorax
func (r *raxRegion) RebootServer(id string, isHard bool) error {
	baseUrl, err := r.EndpointByName("servers")
	serverUrl := fmt.Sprintf("%s/%s/action", baseUrl, id)
	typ := "SOFT"
	if isHard {
		typ = "HARD"
	}
	err = perigee.Post(serverUrl, perigee.Options{
		ReqBody: &struct {
			Reboot struct {
				Type string `json:"type"`
			} `json:"reboot"`
		}{
			struct {
				Type string `json:"type"`
			}{typ},
		},
		OkCodes: []int{202},
		MoreHeaders: map[string]string{
			"X-Auth-Token": r.token,
		},
	})
	return err
}