21
21
testservices.ServiceInstance
22
22
flavors map[string]nova.FlavorDetail
23
23
servers map[string]nova.ServerDetail
24
groups map[int]nova.SecurityGroup
25
rules map[int]nova.SecurityGroupRule
26
floatingIPs map[int]nova.FloatingIP
27
serverGroups map[string][]int
28
serverIPs map[string][]int
24
groups map[string]nova.SecurityGroup
25
rules map[string]nova.SecurityGroupRule
26
floatingIPs map[string]nova.FloatingIP
27
serverGroups map[string][]string
28
serverIPs map[string][]string
75
75
// Real openstack instances have a default security group "out of the box". So we add it here.
76
76
defaultSecurityGroups := []nova.SecurityGroup{
77
{Id: 999, Name: "default", Description: "default group"},
77
{Id: "999", Name: "default", Description: "default group"},
79
79
novaService := &Nova{
80
80
flavors: make(map[string]nova.FlavorDetail),
81
81
servers: make(map[string]nova.ServerDetail),
82
groups: make(map[int]nova.SecurityGroup),
83
rules: make(map[int]nova.SecurityGroupRule),
84
floatingIPs: make(map[int]nova.FloatingIP),
85
serverGroups: make(map[string][]int),
86
serverIPs: make(map[string][]int),
82
groups: make(map[string]nova.SecurityGroup),
83
rules: make(map[string]nova.SecurityGroupRule),
84
floatingIPs: make(map[string]nova.FloatingIP),
85
serverGroups: make(map[string][]string),
86
serverIPs: make(map[string][]string),
87
87
ServiceInstance: testservices.ServiceInstance{
88
88
IdentityService: identityService,
89
89
Hostname: hostname,
370
370
if _, err := n.securityGroup(group.Id); err == nil {
371
return fmt.Errorf("a security group with id %d already exists", group.Id)
371
return fmt.Errorf("a security group with id %s already exists", group.Id)
373
373
group.TenantId = n.TenantId
374
374
if group.Rules == nil {
381
381
// securityGroup retrieves an existing group by ID.
382
func (n *Nova) securityGroup(groupId int) (*nova.SecurityGroup, error) {
382
func (n *Nova) securityGroup(groupId string) (*nova.SecurityGroup, error) {
383
383
if err := n.ProcessFunctionHook(n, groupId); err != nil {
386
386
group, ok := n.groups[groupId]
388
return nil, fmt.Errorf("no such security group %d", groupId)
388
return nil, fmt.Errorf("no such security group %s", groupId)
390
390
return &group, nil
415
415
// removeSecurityGroup deletes an existing group.
416
func (n *Nova) removeSecurityGroup(groupId int) error {
416
func (n *Nova) removeSecurityGroup(groupId string) error {
417
417
if err := n.ProcessFunctionHook(n, groupId); err != nil {
427
427
// addSecurityGroupRule creates a new rule in an existing group.
428
428
// This can be either an ingress or a group rule (see the notes
429
429
// about nova.RuleInfo).
430
func (n *Nova) addSecurityGroupRule(ruleId int, rule nova.RuleInfo) error {
430
func (n *Nova) addSecurityGroupRule(ruleId string, rule nova.RuleInfo) error {
431
431
if err := n.ProcessFunctionHook(n, ruleId, rule); err != nil {
434
434
if _, err := n.securityGroupRule(ruleId); err == nil {
435
return fmt.Errorf("a security group rule with id %d already exists", ruleId)
435
return fmt.Errorf("a security group rule with id %s already exists", ruleId)
437
437
group, err := n.securityGroup(rule.ParentGroupId)
441
441
for _, ru := range group.Rules {
442
442
if ru.Id == ruleId {
443
return fmt.Errorf("cannot add twice rule %d to security group %d", ru.Id, group.Id)
443
return fmt.Errorf("cannot add twice rule %s to security group %s", ru.Id, group.Id)
446
446
var zeroSecurityGroupRef nova.SecurityGroupRef
452
452
if rule.GroupId != nil {
453
453
sourceGroup, err := n.securityGroup(*rule.GroupId)
455
return fmt.Errorf("unknown source security group %d", *rule.GroupId)
455
return fmt.Errorf("unknown source security group %s", *rule.GroupId)
457
457
newrule.Group = nova.SecurityGroupRef{
458
458
TenantId: sourceGroup.TenantId,
482
482
// hasSecurityGroupRule returns whether the given group contains the given rule,
483
// or (when groupId=-1) whether the given rule exists.
484
func (n *Nova) hasSecurityGroupRule(groupId, ruleId int) bool {
483
// or (when groupId="-1") whether the given rule exists.
484
func (n *Nova) hasSecurityGroupRule(groupId, ruleId string) bool {
485
485
rule, ok := n.rules[ruleId]
486
486
_, err := n.securityGroup(groupId)
487
return ok && (groupId == -1 || (err == nil && rule.ParentGroupId == groupId))
487
return ok && (groupId == "-1" || (err == nil && rule.ParentGroupId == groupId))
490
490
// securityGroupRule retrieves an existing rule by ID.
491
func (n *Nova) securityGroupRule(ruleId int) (*nova.SecurityGroupRule, error) {
491
func (n *Nova) securityGroupRule(ruleId string) (*nova.SecurityGroupRule, error) {
492
492
if err := n.ProcessFunctionHook(n, ruleId); err != nil {
495
495
rule, ok := n.rules[ruleId]
497
return nil, fmt.Errorf("no such security group rule %d", ruleId)
497
return nil, fmt.Errorf("no such security group rule %s", ruleId)
499
499
return &rule, nil
502
502
// removeSecurityGroupRule deletes an existing rule from its group.
503
func (n *Nova) removeSecurityGroupRule(ruleId int) error {
503
func (n *Nova) removeSecurityGroupRule(ruleId string) error {
504
504
if err := n.ProcessFunctionHook(n, ruleId); err != nil {
530
530
// addServerSecurityGroup attaches an existing server to a group.
531
func (n *Nova) addServerSecurityGroup(serverId string, groupId int) error {
531
func (n *Nova) addServerSecurityGroup(serverId string, groupId string) error {
532
532
if err := n.ProcessFunctionHook(n, serverId, groupId); err != nil {
543
543
for _, gid := range groups {
544
544
if gid == groupId {
545
return fmt.Errorf("server %q already belongs to group %d", serverId, groupId)
545
return fmt.Errorf("server %q already belongs to group %s", serverId, groupId)
554
554
// hasServerSecurityGroup returns whether the given server belongs to the group.
555
func (n *Nova) hasServerSecurityGroup(serverId string, groupId int) bool {
555
func (n *Nova) hasServerSecurityGroup(serverId string, groupId string) bool {
556
556
if _, err := n.server(serverId); err != nil {
588
588
// removeServerSecurityGroup detaches an existing server from a group.
589
func (n *Nova) removeServerSecurityGroup(serverId string, groupId int) error {
589
func (n *Nova) removeServerSecurityGroup(serverId string, groupId string) error {
590
590
if err := n.ProcessFunctionHook(n, serverId, groupId); err != nil {
611
return fmt.Errorf("server %q does not belong to group %d", serverId, groupId)
611
return fmt.Errorf("server %q does not belong to group %s", serverId, groupId)
613
613
groups = append(groups[:idx], groups[idx+1:]...)
614
614
n.serverGroups[serverId] = groups
623
623
if _, err := n.floatingIP(ip.Id); err == nil {
624
return fmt.Errorf("a floating IP with id %d already exists", ip.Id)
624
return fmt.Errorf("a floating IP with id %s already exists", ip.Id)
626
626
n.floatingIPs[ip.Id] = ip
643
643
// floatingIP retrieves the floating IP by ID.
644
func (n *Nova) floatingIP(ipId int) (*nova.FloatingIP, error) {
644
func (n *Nova) floatingIP(ipId string) (*nova.FloatingIP, error) {
645
645
if err := n.ProcessFunctionHook(n, ipId); err != nil {
648
648
ip, ok := n.floatingIPs[ipId]
650
return nil, fmt.Errorf("no such floating IP %d", ipId)
650
return nil, fmt.Errorf("no such floating IP %s", ipId)
677
677
// removeFloatingIP deletes an existing floating IP by ID.
678
func (n *Nova) removeFloatingIP(ipId int) error {
678
func (n *Nova) removeFloatingIP(ipId string) error {
679
679
if err := n.ProcessFunctionHook(n, ipId); err != nil {
689
689
// addServerFloatingIP attaches an existing floating IP to a server.
690
func (n *Nova) addServerFloatingIP(serverId string, ipId int) error {
690
func (n *Nova) addServerFloatingIP(serverId string, ipId string) error {
691
691
if err := n.ProcessFunctionHook(n, serverId, ipId); err != nil {
707
707
for _, fipId := range fips {
708
708
if fipId == ipId {
709
return fmt.Errorf("server %q already has floating IP %d", serverId, ipId)
709
return fmt.Errorf("server %q already has floating IP %s", serverId, ipId)
736
736
// removeServerFloatingIP deletes an attached floating IP from a server.
737
func (n *Nova) removeServerFloatingIP(serverId string, ipId int) error {
737
func (n *Nova) removeServerFloatingIP(serverId string, ipId string) error {
738
738
if err := n.ProcessFunctionHook(n, serverId); err != nil {
763
return fmt.Errorf("server %q does not have floating IP %d", serverId, ipId)
763
return fmt.Errorf("server %q does not have floating IP %s", serverId, ipId)
765
765
fips = append(fips[:idx], fips[idx+1:]...)
766
766
n.serverIPs[serverId] = fips