func doMoveEIP(region string) { // EIP listing allocIds, err := myec2.ChooseEIP(region) if len(allocIds) == 0 { log.Fatalf("error during selecting to EIP: %s", err.Error()) } // to instance h, err := NewRnzooCStoreManager() if err != nil { log.Printf("can not load EC2: %s\n", err.Error()) } ids, err := h.ChooseEC2(region, myec2.EC2_STATE_ANY, true) if err != nil { log.Fatalf("error during selecting: %s", err.Error()) return } // one instance instanceId := "" if len(ids) >= 1 { instanceId = *ids[0] } else { log.Fatalf("error during selecting to instance: %s", err.Error()) } // moving cli := ec2.New(session.New(), &aws.Config{Region: aws.String(region)}) assocId, err := myec2.AssociateEIP(cli, allocIds[0].AllocationId, instanceId) if err != nil { log.Fatalf("error during moving EIP: %s", err.Error()) } log.Printf("associated association_id:%s\tpublic_ip:%s\tinstance_id:%s", convertNilString(assocId), "EIP", instanceId) }
// allocate new EIP and associate. func doAttachEIP(c *cli.Context) { prepare(c) // load config config, err := GetDefaultConfig() if err != nil { log.Printf("can not load rnzoo config: %s\n", err.Error()) } region := config.AWSRegion moving := c.Bool(OPT_MOVE) if moving { doMoveEIP(region) os.Exit(0) } instanceId := c.String(OPT_INSTANCE_ID) if instanceId == "" { h, err := NewRnzooCStoreManager() if err != nil { log.Printf("can not load EC2: %s\n", err.Error()) } ids, err := h.ChooseEC2(region, myec2.EC2_STATE_ANY, true) if err != nil { log.Fatalf("error during selecting: %s", err.Error()) return } // one instance if len(ids) >= 1 { instanceId = *ids[0] } } else { err := validateInstanceId(instanceId) if err != nil { log.Fatalf("invalid instance id format: %s", err.Error()) } } reuseEIP := c.Bool(OPT_REUSE) cli := ec2.New(session.New(), &aws.Config{Region: aws.String(region)}) var allocId string var ip string if reuseEIP { address, err := myec2.GetNotAssociateEIP(cli) if err != nil { log.Printf("failed no associate address so allocate new address...") } // if exists EIP if address != nil { allocId = convertNilString(address.AllocationId) ip = convertNilString(address.PublicIp) } } if allocId == "" { aid, pip, err := myec2.AllocateEIP(cli) if err != nil { log.Fatalf("failed allocation address:%s", err.Error()) } allocId = convertNilString(aid) ip = convertNilString(pip) log.Printf("allocated allocation_id:%s\tpublic_ip:%s", allocId, ip) } associationId, err := myec2.AssociateEIP(cli, allocId, instanceId) if err != nil { log.Fatalf("failed associate address:%s", err.Error()) } log.Printf("associated association_id:%s\tpublic_ip:%s\tinstance_id:%s", convertNilString(associationId), ip, instanceId) }