Beispiel #1
0
// instances returns a mapping from the specified instance IDs to ec2.Instance
// structures. If any of the specified IDs does not refer to a running instance,
// it will cause an error to be returned.
func (v *ebsVolumeSource) instances(instIds []string) (map[string]ec2.Instance, error) {
	instances := make(map[string]ec2.Instance)
	// Can only attach to running instances.
	filter := ec2.NewFilter()
	filter.Add("instance-state-name", "running")
	resp, err := v.ec2.Instances(instIds, filter)
	if err != nil {
		return nil, err
	}
	for j := range resp.Reservations {
		r := &resp.Reservations[j]
		for _, inst := range r.Instances {
			instances[inst.InstanceId] = inst
		}
	}
	// TODO(wallyworld) - retry to allow instances to get to running state.
	if len(instances) < len(instIds) {
		notRunning := set.NewStrings(instIds...)
		for id, _ := range instances {
			notRunning.Remove(id)
		}
		return nil, errors.Errorf(
			"volumes can only be attached to running instances, these instances are not running: %v",
			strings.Join(notRunning.Values(), ","),
		)
	}
	return instances, nil
}
Beispiel #2
0
func (s *stubVPCAPIClient) CheckSingleRouteTablesCall(c *gc.C, vpc *ec2.VPC) {
	var nilIDs []string
	filter := ec2.NewFilter()
	filter.Add("vpc-id", vpc.Id)

	s.Stub.CheckCallNames(c, "RouteTables")
	s.Stub.CheckCall(c, 0, "RouteTables", nilIDs, filter)
	s.Stub.ResetCalls()
}
Beispiel #3
0
func (s *stubVPCAPIClient) CheckSingleInternetGatewaysCall(c *gc.C, vpc *ec2.VPC) {
	var nilIDs []string
	filter := ec2.NewFilter()
	filter.Add("attachment.vpc-id", vpc.Id)

	s.Stub.CheckCallNames(c, "InternetGateways")
	s.Stub.CheckCall(c, 0, "InternetGateways", nilIDs, filter)
	s.Stub.ResetCalls()
}
Beispiel #4
0
// ListVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) ListVolumes() ([]string, error) {
	filter := ec2.NewFilter()
	filter.Add("tag:"+tags.JujuModel, v.modelUUID)
	resp, err := v.ec2.Volumes(nil, filter)
	if err != nil {
		return nil, err
	}
	volumeIds := make([]string, len(resp.Volumes))
	for i, vol := range resp.Volumes {
		volumeIds[i] = vol.Id
	}
	return volumeIds, nil
}
Beispiel #5
0
func getVPCSubnets(apiClient vpcAPIClient, vpc *ec2.VPC) ([]ec2.Subnet, error) {
	filter := ec2.NewFilter()
	filter.Add("vpc-id", vpc.Id)
	response, err := apiClient.Subnets(nil, filter)
	if err != nil {
		return nil, errors.Annotatef(err, "unexpected AWS response getting subnets of VPC %q", vpc.Id)
	}

	if len(response.Subnets) == 0 {
		return nil, vpcNotUsablef(nil, "no subnets found for VPC %q", vpc.Id)
	}

	return response.Subnets, nil
}
Beispiel #6
0
func getVPCRouteTables(apiClient vpcAPIClient, vpc *ec2.VPC) ([]ec2.RouteTable, error) {
	filter := ec2.NewFilter()
	filter.Add("vpc-id", vpc.Id)
	response, err := apiClient.RouteTables(nil, filter)
	if err != nil {
		return nil, errors.Annotatef(err, "unexpected AWS response getting route tables of VPC %q", vpc.Id)
	}

	if len(response.Tables) == 0 {
		return nil, vpcNotRecommendedf("VPC has no route tables")
	}
	logger.Tracef("RouteTables() returned %#v", response)

	return response.Tables, nil
}
Beispiel #7
0
func getVPCInternetGateway(apiClient vpcAPIClient, vpc *ec2.VPC) (*ec2.InternetGateway, error) {
	filter := ec2.NewFilter()
	filter.Add("attachment.vpc-id", vpc.Id)
	response, err := apiClient.InternetGateways(nil, filter)
	if err != nil {
		return nil, errors.Annotatef(err, "unexpected AWS response getting Internet Gateway of VPC %q", vpc.Id)
	}

	if numResults := len(response.InternetGateways); numResults == 0 {
		return nil, vpcNotRecommendedf("VPC has no Internet Gateway attached")
	} else if numResults > 1 {
		logger.Debugf("InternetGateways() returned %#v", response)
		return nil, errors.Errorf("expected 1 result from AWS, got %d", numResults)
	}

	gateway := response.InternetGateways[0]
	return &gateway, nil
}
Beispiel #8
0
func (c instanceCache) update(ec2client *ec2.EC2, ids ...string) error {
	if len(ids) == 1 {
		if _, ok := c[ids[0]]; ok {
			return nil
		}
	}
	filter := ec2.NewFilter()
	filter.Add("instance-state-name", "running")
	resp, err := ec2client.Instances(ids, filter)
	if err != nil {
		return errors.Annotate(err, "querying instance details")
	}
	for j := range resp.Reservations {
		r := &resp.Reservations[j]
		for _, inst := range r.Instances {
			c[inst.InstanceId] = inst
		}
	}
	return nil
}
Beispiel #9
0
Datei: ebs.go Projekt: bac/juju
// ListVolumes is specified on the storage.VolumeSource interface.
func (v *ebsVolumeSource) ListVolumes() ([]string, error) {
	filter := ec2.NewFilter()
	filter.Add("tag:"+tags.JujuModel, v.modelUUID)
	return listVolumes(v.env.ec2, filter)
}