本文整理汇总了Golang中github.com/rackspace/gophercloud/openstack/networking/v2/networks.List函数的典型用法代码示例。如果您正苦于以下问题:Golang List函数的具体用法?Golang List怎么用?Golang List使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了List函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getNetworkName
func getNetworkName(d *schema.ResourceData, meta interface{}, networkID string) (string, error) {
config := meta.(*Config)
networkClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return "", fmt.Errorf("Error creating OpenStack network client: %s", err)
}
opts := networks.ListOpts{ID: networkID}
pager := networks.List(networkClient, opts)
networkName := ""
err = pager.EachPage(func(page pagination.Page) (bool, error) {
networkList, err := networks.ExtractNetworks(page)
if err != nil {
return false, err
}
for _, n := range networkList {
if n.ID == networkID {
networkName = n.Name
return false, nil
}
}
return true, nil
})
return networkName, err
}
示例2: createServer
func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
if testing.Short() {
t.Skip("Skipping test that requires server creation in short mode.")
}
var network networks.Network
networkingClient, err := networkingClient()
if err != nil {
t.Fatalf("Unable to create a networking client: %v", err)
}
pager := networks.List(networkingClient, networks.ListOpts{
Name: choices.NetworkName,
Limit: 1,
})
pager.EachPage(func(page pagination.Page) (bool, error) {
networks, err := networks.ExtractNetworks(page)
if err != nil {
t.Errorf("Failed to extract networks: %v", err)
return false, err
}
if len(networks) == 0 {
t.Fatalf("No networks to attach to server")
return false, err
}
network = networks[0]
return false, nil
})
name := tools.RandomString("ACPTTEST", 16)
t.Logf("Attempting to create server: %s\n", name)
pwd := tools.MakeNewPassword("")
server, err := servers.Create(client, servers.CreateOpts{
Name: name,
FlavorRef: choices.FlavorID,
ImageRef: choices.ImageID,
Networks: []servers.Network{
servers.Network{UUID: network.ID},
},
AdminPass: pwd,
}).Extract()
if err != nil {
t.Fatalf("Unable to create server: %v", err)
}
th.AssertEquals(t, pwd, server.AdminPass)
return server, err
}
示例3: TestNetworkCRUDOperations
func TestNetworkCRUDOperations(t *testing.T) {
Setup(t)
defer Teardown()
// Create a network
n, err := networks.Create(Client, networks.CreateOpts{Name: "sample_network", AdminStateUp: networks.Up}).Extract()
th.AssertNoErr(t, err)
defer networks.Delete(Client, n.ID)
th.AssertEquals(t, n.Name, "sample_network")
th.AssertEquals(t, n.AdminStateUp, true)
networkID := n.ID
// List networks
pager := networks.List(Client, networks.ListOpts{Limit: 2})
err = pager.EachPage(func(page pagination.Page) (bool, error) {
t.Logf("--- Page ---")
networkList, err := networks.ExtractNetworks(page)
th.AssertNoErr(t, err)
for _, n := range networkList {
t.Logf("Network: ID [%s] Name [%s] Status [%s] Is shared? [%s]",
n.ID, n.Name, n.Status, strconv.FormatBool(n.Shared))
}
return true, nil
})
th.CheckNoErr(t, err)
// Get a network
if networkID == "" {
t.Fatalf("In order to retrieve a network, the NetworkID must be set")
}
n, err = networks.Get(Client, networkID).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Status, "ACTIVE")
th.AssertDeepEquals(t, n.Subnets, []string{})
th.AssertEquals(t, n.Name, "sample_network")
th.AssertEquals(t, n.AdminStateUp, true)
th.AssertEquals(t, n.Shared, false)
th.AssertEquals(t, n.ID, networkID)
// Update network
n, err = networks.Update(Client, networkID, networks.UpdateOpts{Name: "new_network_name"}).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, n.Name, "new_network_name")
// Delete network
res := networks.Delete(Client, networkID)
th.AssertNoErr(t, res.Err)
}
示例4: getNetworkId
func (c *GenericClient) getNetworkId(d *Driver, networkName string) (string, error) {
opts := networks.ListOpts{Name: networkName}
pager := networks.List(c.Network, opts)
networkId := ""
err := pager.EachPage(func(page pagination.Page) (bool, error) {
networkList, err := networks.ExtractNetworks(page)
if err != nil {
return false, err
}
for _, n := range networkList {
if n.Name == networkName {
networkId = n.ID
return false, nil
}
}
return true, nil
})
return networkId, err
}
示例5: List
// List returns a Pager which allows you to iterate over a collection of
// networks. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
func List(c *gophercloud.ServiceClient, opts os.ListOptsBuilder) pagination.Pager {
return os.List(c, opts)
}
示例6: TestList
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/v2.0/networks", 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")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"networks": [
{
"status": "ACTIVE",
"subnets": [
"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"
],
"name": "private-network",
"admin_state_up": true,
"tenant_id": "4fd44f30292945e481c7b8a0c8908869",
"shared": true,
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"provider:segmentation_id": null,
"provider:physical_network": null,
"provider:network_type": "local"
},
{
"status": "ACTIVE",
"subnets": [
"08eae331-0402-425a-923c-34f7cfe39c1b"
],
"name": "private",
"admin_state_up": true,
"tenant_id": "26a7980765d0414dbc1fc1f88cdb7e6e",
"shared": true,
"id": "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
"provider:segmentation_id": 1234567890,
"provider:physical_network": null,
"provider:network_type": "local"
}
]
}
`)
})
count := 0
networks.List(fake.ServiceClient(), networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractList(page)
if err != nil {
t.Errorf("Failed to extract networks: %v", err)
return false, err
}
expected := []NetworkExtAttrs{
NetworkExtAttrs{
Status: "ACTIVE",
Subnets: []string{"54d6f61d-db07-451c-9ab3-b9609b6b6f0b"},
Name: "private-network",
AdminStateUp: true,
TenantID: "4fd44f30292945e481c7b8a0c8908869",
Shared: true,
ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22",
NetworkType: "local",
PhysicalNetwork: "",
SegmentationID: "",
},
NetworkExtAttrs{
Status: "ACTIVE",
Subnets: []string{"08eae331-0402-425a-923c-34f7cfe39c1b"},
Name: "private",
AdminStateUp: true,
TenantID: "26a7980765d0414dbc1fc1f88cdb7e6e",
Shared: true,
ID: "db193ab3-96e3-4cb3-8fc5-05f4296d0324",
NetworkType: "local",
PhysicalNetwork: "",
SegmentationID: "1234567890",
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}
示例7: main
func main() {
// step-1
var authUsername string = "your_auth_username"
var authPassword string = "your_auth_password"
var authUrl string = "http://controller:5000"
var projectName string = "your_project_id"
var regionName string = "your_region_name"
authOpts := gophercloud.AuthOptions{
IdentityEndpoint: authUrl,
Username: authUsername,
Password: authPassword,
TenantID: projectName,
}
provider, _ := openstack.AuthenticatedClient(authOpts)
client, _ := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: regionName,
})
// step-2
pager := images.ListDetail(client, images.ListOpts{})
page, _ := pager.AllPages()
imageList, _ := images.ExtractImages(page)
fmt.Println(imageList)
// step-3
pager = flavors.ListDetail(client, flavors.ListOpts{})
page, _ = pager.AllPages()
flavorList, _ := flavors.ExtractFlavors(page)
fmt.Println(flavorList)
// step-4
imageID := "74e6d1ec-9a08-444c-8518-4f232446386d"
image, _ := images.Get(client, imageID).Extract()
fmt.Println(image)
// step-5
flavorID := "1"
flavor, _ := flavors.Get(client, flavorID).Extract()
fmt.Println(flavor)
// step-6
instanceName := "testing"
testingInstance, _ := servers.Create(client, servers.CreateOpts{
Name: instanceName,
ImageRef: imageID,
FlavorRef: flavorID,
}).Extract()
fmt.Println(testingInstance)
// step-7
pager = servers.List(client, servers.ListOpts{})
page, _ = pager.AllPages()
serverList, _ := servers.ExtractServers(page)
fmt.Println(serverList)
// step-8
servers.Delete(client, testingInstance.ID)
// step-9
fmt.Println("Checking for existing SSH key pair...")
keyPairName := "demokey"
pubKeyFile := "~/.ssh/id_rsa.pub"
keyPairExists := false
pager = keypairs.List(client)
page, _ = pager.AllPages()
keypairList, _ := keypairs.ExtractKeyPairs(page)
for _, k := range keypairList {
if k.Name == keyPairName {
keyPairExists = true
break
}
}
if keyPairExists {
fmt.Println("Keypair " + keyPairName + " already exists. Skipping import.")
} else {
fmt.Println("adding keypair...")
bs, _ := ioutil.ReadFile(pubKeyFile)
keypairs.Create(client, keypairs.CreateOpts{
Name: keyPairName,
PublicKey: string(bs),
}).Extract()
}
pager = keypairs.List(client)
page, _ = pager.AllPages()
keypairList, _ = keypairs.ExtractKeyPairs(page)
fmt.Println(keypairList)
// step-10
fmt.Println("Checking for existing security group...")
var allInOneSecurityGroup secgroups.SecurityGroup
securityGroupName := "all-in-one"
securityGroupExists := false
pager = secgroups.List(client)
page, _ = pager.AllPages()
secgroupList, _ := secgroups.ExtractSecurityGroups(page)
//.........这里部分代码省略.........
示例8: TestList
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/networks", 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")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"networks": [
{
"admin_state_up": true,
"id": "0f38d5ad-10a6-428f-a5fc-825cfe0f1970",
"name": "net1",
"router:external": false,
"shared": false,
"status": "ACTIVE",
"subnets": [
"25778974-48a8-46e7-8998-9dc8c70d2f06"
],
"tenant_id": "b575417a6c444a6eb5cc3a58eb4f714a"
},
{
"admin_state_up": true,
"id": "8d05a1b1-297a-46ca-8974-17debf51ca3c",
"name": "ext_net",
"router:external": true,
"shared": false,
"status": "ACTIVE",
"subnets": [
"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"
],
"tenant_id": "5eb8995cf717462c9df8d1edfa498010"
}
]
}
`)
})
count := 0
networks.List(fake.ServiceClient(), networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractList(page)
if err != nil {
t.Errorf("Failed to extract networks: %v", err)
return false, err
}
expected := []NetworkExternal{
NetworkExternal{
Status: "ACTIVE",
Subnets: []string{"25778974-48a8-46e7-8998-9dc8c70d2f06"},
Name: "net1",
AdminStateUp: true,
TenantID: "b575417a6c444a6eb5cc3a58eb4f714a",
Shared: false,
ID: "0f38d5ad-10a6-428f-a5fc-825cfe0f1970",
External: false,
},
NetworkExternal{
Status: "ACTIVE",
Subnets: []string{"2f1fb918-9b0e-4bf9-9a50-6cebbb4db2c5"},
Name: "ext_net",
AdminStateUp: true,
TenantID: "5eb8995cf717462c9df8d1edfa498010",
Shared: false,
ID: "8d05a1b1-297a-46ca-8974-17debf51ca3c",
External: true,
},
}
th.CheckDeepEquals(t, expected, actual)
return true, nil
})
if count != 1 {
t.Errorf("Expected 1 page, got %d", count)
}
}