func TestFlavorsGet(t *testing.T) { client, err := clients.NewComputeV2Client() if err != nil { t.Fatalf("Unable to create a compute client: %v", err) } choices, err := clients.AcceptanceTestChoicesFromEnv() if err != nil { t.Fatal(err) } flavor, err := flavors.Get(client, choices.FlavorID).Extract() if err != nil { t.Fatalf("Unable to get flavor information: %v", err) } PrintFlavor(t, flavor) }
func TestGetFlavor(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/flavors/12345", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "GET") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.Header().Add("Content-Type", "application/json") fmt.Fprintf(w, ` { "flavor": { "id": "1", "name": "m1.tiny", "disk": 1, "ram": 512, "vcpus": 1, "rxtx_factor": 1, "swap": "" } } `) }) actual, err := flavors.Get(fake.ServiceClient(), "12345").Extract() if err != nil { t.Fatalf("Unable to get flavor: %v", err) } expected := &flavors.Flavor{ ID: "1", Name: "m1.tiny", Disk: 1, RAM: 512, VCPUs: 1, RxTxFactor: 1, Swap: 0, } if !reflect.DeepEqual(expected, actual) { t.Errorf("Expected %#v, but was %#v", expected, actual) } }
func (s *StepLoadFlavor) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(Config) ui := state.Get("ui").(packer.Ui) // We need the v2 compute client client, err := config.computeV2Client() if err != nil { err = fmt.Errorf("Error initializing compute client: %s", err) state.Put("error", err) return multistep.ActionHalt } ui.Say(fmt.Sprintf("Loading flavor: %s", s.Flavor)) log.Printf("[INFO] Loading flavor by ID: %s", s.Flavor) flavor, err := flavors.Get(client, s.Flavor).Extract() if err != nil { log.Printf("[ERROR] Failed to find flavor by ID: %s", err) geterr := err log.Printf("[INFO] Loading flavor by name: %s", s.Flavor) id, err := flavors.IDFromName(client, s.Flavor) if err != nil { log.Printf("[ERROR] Failed to find flavor by name: %s", err) err = fmt.Errorf( "Unable to find specified flavor by ID or name!\n\n"+ "Error from ID lookup: %s\n\n"+ "Error from name lookup: %s", geterr, err) state.Put("error", err) return multistep.ActionHalt } flavor = &flavors.Flavor{ID: id} } ui.Message(fmt.Sprintf("Verified flavor. ID: %s", flavor.ID)) state.Put("flavor_id", flavor.ID) return multistep.ActionContinue }
func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) computeClient, err := config.computeV2Client(GetRegion(d)) if err != nil { return fmt.Errorf("Error creating OpenStack compute client: %s", err) } server, err := servers.Get(computeClient, d.Id()).Extract() if err != nil { return CheckDeleted(d, err, "server") } log.Printf("[DEBUG] Retrieved Server %s: %+v", d.Id(), server) d.Set("name", server.Name) // Get the instance network and address information networks, err := getInstanceNetworksAndAddresses(computeClient, d) if err != nil { return err } // Determine the best IPv4 and IPv6 addresses to access the instance with hostv4, hostv6 := getInstanceAccessAddresses(d, networks) if server.AccessIPv4 != "" && hostv4 == "" { hostv4 = server.AccessIPv4 } if server.AccessIPv6 != "" && hostv6 == "" { hostv6 = server.AccessIPv6 } d.Set("network", networks) d.Set("access_ip_v4", hostv4) d.Set("access_ip_v6", hostv6) // Determine the best IP address to use for SSH connectivity. // Prefer IPv4 over IPv6. preferredSSHAddress := "" if hostv4 != "" { preferredSSHAddress = hostv4 } else if hostv6 != "" { preferredSSHAddress = hostv6 } if preferredSSHAddress != "" { // Initialize the connection info d.SetConnInfo(map[string]string{ "type": "ssh", "host": preferredSSHAddress, }) } d.Set("metadata", server.Metadata) secGrpNames := []string{} for _, sg := range server.SecurityGroups { secGrpNames = append(secGrpNames, sg["name"].(string)) } d.Set("security_groups", secGrpNames) flavorId, ok := server.Flavor["id"].(string) if !ok { return fmt.Errorf("Error setting OpenStack server's flavor: %v", server.Flavor) } d.Set("flavor_id", flavorId) flavor, err := flavors.Get(computeClient, flavorId).Extract() if err != nil { return err } d.Set("flavor_name", flavor.Name) // Set the instance's image information appropriately if err := setImageInformation(computeClient, server, d); err != nil { return err } // volume attachments if err := getVolumeAttachments(computeClient, d); err != nil { return err } return nil }