文档首页> > 开发指南> Go> VPC Go SDK用户指导

VPC Go SDK用户指导

更新时间: 2018-11-26 14:49

Service Client

为了能够与OpenStack API进行交互,首先必须将身份验证凭据传递给Provider,此后,如果要检索Network服务的任何信息,则需要调用NewNetworkV2方法来创建一个Network服务客户端,然后即可使用相关的SDK。

client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
 Name:   "neutron",
 Region: "RegionName",
})

Networks

网络是OpenStack Neutron API的核心资源。与物理网络对比,Neutron网络近似于一个VLAN,是一个在更大的二层网络中独立的广播域。由于是虚拟化分区,虚拟网络只能通过一个或多个路由器与其他网络共享数据包。

Create a network 创建网络

可使用指定名称创建网络。

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
)
func TestNetworksCreate(t *testing.T) {
createOpts := networks.CreateOpts{
    Name               : "Sample",
}

// Execute the operation and get back a networks.Network struct
network, err := networks.Create(client, createOpts).Extract()
}

List networks 查询网络列表

要检索指定网络的信息,需使用网络名称。返回的networks.Network结构具有IDNameStatusSubnetsSharedTenandID字段。

func TestNetworksListByName(t *testing.T) {

// We have the option of filtering the network list. If we want the full
// collection, leave it as an empty struct
opts := networks.ListOpts{Name: "Sample"}

// Retrieve a pager (i.e. a paginated collection)
allPages, err := networks.List(client, listOpts).AllPages()

// Define an anonymous function to be executed on each page's iteration
var allNetworks []networks.ListOpts
err := networks.ExtractNetworksInto(allPages, &allNetworks)

for _, network := range allNetworks {
    // " network " will be a networks.Network
}
}

Get details for an existing network 获取网络详情

使用UUID可获取指定网络的信息。

func TestNetworksShow(t *testing.T) {
// We need to know what the UUID of our network is and pass it in as a string
network, err := networks.Get(client, “network_id”).Extract()
}

Update an existing network 更新现有网络信息

可更新网络名称。

func TestNetworksUpdate(t *testing.T) {

updateOpts := &networks.UpdateOpts{
    Name    : "Sample",
     Shared  : true,
}

// Like Get(), we need the UUID in string form
network, err := networks.Update(client, "network_id", updateOpts).Extract()
}

Delete a network 删除网络

可使用UUID删除指定网络。

func TestNetworksDelete(t *testing.T) {

// We need to know what the UUID of our network is and pass it in as a string
err := networks.Delete(client, "network_id").ExtractErr()
}

Subnets

子网是IP地址块,IP地址被分配给网络中的设备。在Neutron中,设备是指虚拟机(计算实例),每个子网都必须有CIDR并与某个网络相关联。

Create a subnet 创建子网

使用网络名称和CIDR创建子网:

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/gophercloud/pagination"
)

func TestSubnetCreate(t *testing.T) {

// You must associate a new subnet with an existing network - to do this you
// need its UUID. You must also provide a well-formed CID
createOpts := subnets.CreateOpts{
     Name           :  "Sample",
     NetworkID      :  "network_id",
     CIDR           :  subnetCIDR,
}

// Execute the operation and get back a subnets.Subnet struct
subnet, err := subnets.Create(client, createOpts).Extract()
}

List subnets 查询子网列表

要检索子网的信息,需要使用与该子网相关联的网络名称(字符串格式)。返回的subnets.Subnet结构具有IDNameCidrTenandID字段。

func TestSubnetsListByNetID(t *testing.T) {

// We have the option of filtering subnets. For example, we may want to return
// every subnet that belongs to a specific network. Or filter again by name.
opts := subnets.ListOpts{NetworkID: "some_uuid"}

// Retrieve a pager (i.e. a paginated collection)
pager := subnets.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    subnetList, err := subnets.ExtractSubnets(page)

    for _, subnet := range subnetList {
           // "subnet" will be a subnets.Subnet
    }
})
}

Get details for an existing subnet 获取现有子网详情

可使用UUID获取特定子网信息。

func TestSubnetShow(t *testing.T) {
// We need to know what the UUID of our subnet is and pass it in as a string
subnet, err := subnets.Get(client, "subnet_id").Extract()
}

Update an existing subnet 更新现有子网

可更新子网名称、网关IP地址、DNS域名服务器、主机路由及“enable DHCP”状态。

func TestSubnetUpdate(t *testing.T) {

opts := subnets.UpdateOpts{Name: "Sample"}
subnet, err := subnets.Update(client, "subnet_id", opts).Extract() 
}

Delete a subnet 删除子网

使用UUID删除特定子网。

func TestSubnetDelete(t *testing.T) {

// We need to know what the UUID of our network is and pass it in as a string
result := subnets.Delete(client, "subnet_id") 
}

Ports

了解什么是端口,首先要知道一个重要的概念:什么是网络交换机(包括虚拟及物理交换机)。网络交换机将两个不同的网段连接在一起,其中端口就是设备与交换机连接的地方。在这里,设备是指虚拟机。请参阅相关文献了解这些术语知识。

Create a port 创建端口

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/pagination"
)

func TestPortsCreate(t *testing.T) {

// You must associate a new port with an existing network - to do this you
// need its UUID. Also notice the "FixedIPs" field; this allows you to specify
// either a specific IP to use for this port, or the subnet ID from which a
// random free IP is selected.
opts := ports.CreateOpts{
    NetworkID:    "network_id",
    Name:         "my_port",
    AdminStateUp: ports.Up,
    FixedIPs:     []ports.IP{ports.IP{SubnetID: "subnet_id"}},
}

// Execute the operation and get back a subnets.Subnet struct
port, err := ports.Create(client, opts).Extract()
}

List ports 查询端口列表

func TestPortsListByNetID(t *testing.T) {

// We have the option of filtering ports. For example, we may want to return
// every port that belongs to a specific network. Or filter again by MAC address.
opts := ports.ListOpts{NetworkID: "some_uuid", MACAddress: "some_addr"}

// Retrieve a pager (i.e. a paginated collection)
pager := ports.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    portList, err := ports.ExtractPorts(page)

    for _, port := range portList {
           // "port" will be a ports.Port
    }
})
}

Get details for an existing port 获取现有端口详情

可使用UUID获取特定端口信息。

func TestPortShow(t *testing.T) {
// We need to know what the UUID of our port is and pass it in as a string
port, err := ports.Get(client, "port_id").Extract()
}

Update an existing port 更新现有端口

可以更新端口名称、管理状态、固定IP、设备ID、安全组等。

func TestPortUpdate(t *testing.T) {

opts := ports.UpdateOpts{Name: "new_port_name"}
port, err := ports.Update(client, "port_id", opts).Extract()
}

Delete a port 删除端口

可使用UUID删除特定端口。

func TestPortDelete(t *testing.T) {

// We need to know what the UUID of our network is and pass it in as a string
result := ports.Delete(client, "port_id").ExtractErr()
}

Routers

Create a router 创建路由器

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
networking "github.com/gophercloud/gophercloud/OTCtest/openstack/networking/v2"
"github.com/gophercloud/gophercloud/pagination"
)

func TestLayer3ExternalRouterCreate(t *testing.T) {
// You can specify router pamameter
createOpts := routers.CreateOpts{
    Name                 : "Sample",
}
// Execute the operation and get back a routers.Router struct
router, err := routers.Create(client, createOpts).Extract()
}

Add interface to router 添加路由器接口

func TestLayer3RouterAddInterfaceByPortID(t *testing.T) {
    // You can specify router pamameter
    createOpts := routers.CreateOpts{
      Name               : "Sample",
    }
     // Execute the operation and get back a routers.Router struct
     router, err = routers.Create(client, createOpts).Extract()

     // Create port
     port, err := networking.CreatePort(t, client, network.ID, subnet.ID)
    
    // add interface to router
    aiOpts := routers.AddInterfaceOpts{
           PortID: "port_id",
    }
     iface, err := routers.AddInterface(client, router.ID, aiOpts).Extract()
}

Remove interface from router 移除路由器接口

func TestLayer3RouterAddInterfaceByPortID(t *testing.T) {
    // remove interface to router
    aiOpts := routers.RemoveInterfaceOpts{
           PortID: "port_id",
    }
     iface, err := routers.RemoveInterface(client, "router_id", aiOpts).Extract()
}

List routers 查询路由器列表

func TestLayer3RouterListByName(t *testing.T) {

// We have the option of filtering routers. For example, we may want to 
// filter by name.
listOpts := routers.ListOpts{
    Name: router.Name,
}

// Retrieve a pager (i.e. a paginated collection)
pager := routers.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    routerList, err := routers.ExtractRouters(page)

    for _, router := range routerList {
           // " router " will be a routers.Router
    }
})
}

Get details for an existing router 获取现存路由器详情

可以使用UUID获取特定路由器信息。

func TestLayer3RouterShow(t *testing.T) {
// We need to know what the UUID of our routers is and pass it in as a string
subnet, err := routers.Get(client, "router_id").Extract()
}

Update an existing router 更新路由器

func TestLayer3ExternalRouterUpdate(t *testing.T) {

opts := routers.UpdateOpts{Name: "Sample"}
subnet, err := routers.Update(client, "router_id", opts).Extract() 
}

Delete a router 删除路由器

可使用UIDD删除特定路由器。

func TestLayer3RouterDelete (t *testing.T) {

// We need to know what the UUID of our routers is and pass it in as a string
result := routers.Delete(client, "router_id").ExtractErr()
}

FloatingIP

Create a floatingip 创建浮动IP

import(
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/pagination"
)

func TestLayer3FloatingIPsCreate(t *testing.T) {

createOpts := &floatingips.CreateOpts{
    FloatingNetworkID: "external_network_id",
}
// Execute the operation and get back a floatingips.Floatingip struct
floatingIP, err := floatingips.Create(client, createOpts).Extract()
}

List floatingips 查询浮动IP列表

func TestLayer3FloatingIPsListByID(t *testing.T) {

listOpts := floatingips.ListOpts{
    ID: "floatingip_id",
}

// Retrieve a pager (i.e. a paginated collection)
pager := floatingips.List(client, opts)

// Define an anonymous function to be executed on each page's iteration
err := pager.EachPage(func(page pagination.Page) (bool, error) {
    floatingipList, err := floatingips.ExtractFloatingIPs(page)

    for _, floatingip:= range floatingipList {
           // "floatingip" will be a floatingips.Floatingip
    }
})
}

Get details for an existing floatingip 获取现有浮动IP详情

可使用UUID获取特定浮动IP信息。

func TestLayer3FloatingIPShow(t *testing.T) {
// We need to know what the UUID of our floatingip is and pass it in as a string
floatingip, err := floatingips.Get(client, "floatingip_id").Extract()
}

Update an existing floatingip 更新现有浮动IP

func TestLayer3FloatingIPsUpdate(t *testing.T) {
opts := floatingips.UpdateOpts{Name: "Sample"}
floatingip, err := floatingips.Update(client, "floatingip_id", opts).Extract() 
}

Delete a floatingip 删除浮动IP

可使用UUID删除特定浮动IP。

func TestLayer3FloatingIPsDelete(t *testing.T) {

// We need to know what the UUID of our floatingip is and pass it in as a string
result := floatingip.Delete(client, "floatingip_id").ExtractErr()
}

SecurityGroups

Create a security group 创建安全组

import (   "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"   ) 

// Create is an operation which provisions a new security group with default
// security group rules for the IPv4 and IPv6 ether types.
func TestSecGroupsCreate(t *testing.T) {
createOpts:= groups. CreateOpts{Name: "Security", Description: "Create Group"} 
secgroup, err := groups.Create(client, createOpts).Extract()
}

List Security Groups 查询安全组列表

func TestCreateSecGroupsList(t *testing.T) {
// We have the option of filtering the Security Groups list. If we want the full       
// collection, leave it as an empty struct       
opts := groups.ListOpts{TenantID: "tenant_id", limit: 2}         
// Retrieve a pager (i.e. a paginated collection)      allPages,err:= groups.List(client, opts).AllPages()    // Define an anonymous function to be executed on each page's iteration      
securityGroupsList, err := groups.ExtractGroups(allPages)         
for _, secgroup := range securityGroupsList {      
// " secgroup " will be a groups.SecGroup      
} 
}

Get details for a specific security group 获取特定安全组详情

Get可根据安全组唯一ID对特定安全组进行检索。

func TestCreateSecGroupsShow(t *testing.T) {
// Get back a groups.Secgroup struct   
group, err := groups.Get(client, "secgroup_id").Extract()
}

Update a security group 更新安全组

func TestSecGroupsUpdate(t *testing.T) {

// Update is an operation which updates an existing security group.  
ops:= groups.UpdateOpts{Name:"UpdateGroup", Description:"Update a Group"}
updatedSecurityGroup, err := groups.Update(client, "secgroup_id", updateOpts).Extract()
}

Security Group Rules

Create a security group rule 创建安全组规则

Create操作可添加新的安全组规则并将其与现有安全组相关联。现有安全组ID是通过CreateOpts指定的。

import (  
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"   ) 

func TestCreateSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient) {
    createOpts := rules.CreateOpts{
           Direction:    "ingress",
           EtherType:    "IPv4",
           SecGroupID:   "secGroup_id",
           PortRangeMin: 10,
           PortRangeMax: 20,
           Protocol:     rules.ProtocolTCP,
    }
    rule, err := rules.Create(client, createOpts).Extract()
}

List security group rules 查询安全组规则列表

通过List可返回安全组规则列表。用户可通过ListOpts配置过滤规则,提高效率。

func TestSecGroupsRuleListBySgID(t *testing.T){
    ListOpts := rules.ListOpts{
           SecGroupID : "secgroup_id",
    }
    // Retrieve a pager (i.e. a paginated collection)
    allPages, err := rules.List(client, ListOpts).AllPages()

    // Define an anonymous function to be executed on each page's iteration
    allRules, err := rules.ExtractRules(allPages)

                                for _, rule := range allRules {
    // " rule " will be a rules.SecGroupRule
    }
}

Get details for a specific security group rule 获取特定安全组规则详情

Get可根据唯一ID检索特定安全组规则。

func TestSecGroupsRuleShow(t *testing.T) {
    SecurityGroupRule, err := rules.Get(client, "securitygrouprule_id").Extract()
      }

Delete a security group rule 删除安全组规则

通过Delete可根据唯一ID永久性删除特定安全组规则。

func TestDeleteSecurityGroupRule(t *testing.T, client *gophercloud.ServiceClient{
err := rules.Delete(client,"rule_id").ExtractErr()
}

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

在文档使用中是否遇到以下问题







请至少选择或填写一项反馈信息

字符长度不能超过100

反馈内容不能为空!

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区
点我,12·12惊喜等着你哦~