~bigkevmcd/goose/fix-for-swift-headers

« back to all changes in this revision

Viewing changes to testservices/novaservice/service.go

[r=wallyworld],[bug=1188815] Support string id for SecurityGroup et al

Openstack with Quantum/Grizzly uses string ids for SecurityGroup
and other similar data structures. Goose was written for deployments
that use int ids. This branch fixes that issue using the pattern
already in palce to deal with HP Cloud vs Canonistack for
ServerDetail et al ids.

https://codereview.appspot.com/10527043/

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
29
29
        nextServerId int
30
30
        nextGroupId  int
31
31
        nextRuleId   int
74
74
        }
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"},
78
78
        }
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,
368
368
                return err
369
369
        }
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)
372
372
        }
373
373
        group.TenantId = n.TenantId
374
374
        if group.Rules == nil {
379
379
}
380
380
 
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 {
384
384
                return nil, err
385
385
        }
386
386
        group, ok := n.groups[groupId]
387
387
        if !ok {
388
 
                return nil, fmt.Errorf("no such security group %d", groupId)
 
388
                return nil, fmt.Errorf("no such security group %s", groupId)
389
389
        }
390
390
        return &group, nil
391
391
}
413
413
}
414
414
 
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 {
418
418
                return err
419
419
        }
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 {
432
432
                return err
433
433
        }
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)
436
436
        }
437
437
        group, err := n.securityGroup(rule.ParentGroupId)
438
438
        if err != nil {
440
440
        }
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)
444
444
                }
445
445
        }
446
446
        var zeroSecurityGroupRef nova.SecurityGroupRef
452
452
        if rule.GroupId != nil {
453
453
                sourceGroup, err := n.securityGroup(*rule.GroupId)
454
454
                if err != nil {
455
 
                        return fmt.Errorf("unknown source security group %d", *rule.GroupId)
 
455
                        return fmt.Errorf("unknown source security group %s", *rule.GroupId)
456
456
                }
457
457
                newrule.Group = nova.SecurityGroupRef{
458
458
                        TenantId: sourceGroup.TenantId,
480
480
}
481
481
 
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))
488
488
}
489
489
 
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 {
493
493
                return nil, err
494
494
        }
495
495
        rule, ok := n.rules[ruleId]
496
496
        if !ok {
497
 
                return nil, fmt.Errorf("no such security group rule %d", ruleId)
 
497
                return nil, fmt.Errorf("no such security group rule %s", ruleId)
498
498
        }
499
499
        return &rule, nil
500
500
}
501
501
 
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 {
505
505
                return err
506
506
        }
528
528
}
529
529
 
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 {
533
533
                return err
534
534
        }
542
542
        if ok {
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)
546
546
                        }
547
547
                }
548
548
        }
552
552
}
553
553
 
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 {
557
557
                return false
558
558
        }
586
586
}
587
587
 
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 {
591
591
                return err
592
592
        }
608
608
                }
609
609
        }
610
610
        if idx == -1 {
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)
612
612
        }
613
613
        groups = append(groups[:idx], groups[idx+1:]...)
614
614
        n.serverGroups[serverId] = groups
621
621
                return err
622
622
        }
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)
625
625
        }
626
626
        n.floatingIPs[ip.Id] = ip
627
627
        return nil
641
641
}
642
642
 
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 {
646
646
                return nil, err
647
647
        }
648
648
        ip, ok := n.floatingIPs[ipId]
649
649
        if !ok {
650
 
                return nil, fmt.Errorf("no such floating IP %d", ipId)
 
650
                return nil, fmt.Errorf("no such floating IP %s", ipId)
651
651
        }
652
652
        return &ip, nil
653
653
}
675
675
}
676
676
 
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 {
680
680
                return err
681
681
        }
687
687
}
688
688
 
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 {
692
692
                return err
693
693
        }
706
706
        if ok {
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)
710
710
                        }
711
711
                }
712
712
        }
734
734
}
735
735
 
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 {
739
739
                return err
740
740
        }
760
760
                }
761
761
        }
762
762
        if idx == -1 {
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)
764
764
        }
765
765
        fips = append(fips[:idx], fips[idx+1:]...)
766
766
        n.serverIPs[serverId] = fips