本文整理汇总了Golang中github.com/Juniper/contrail-go-api/types.VirtualNetwork类的典型用法代码示例。如果您正苦于以下问题:Golang VirtualNetwork类的具体用法?Golang VirtualNetwork怎么用?Golang VirtualNetwork使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了VirtualNetwork类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: locatePolicyRule
func (m *ServiceManagerImpl) locatePolicyRule(policy *types.NetworkPolicy, lhs, rhs *types.VirtualNetwork) error {
lhsName := strings.Join(lhs.GetFQName(), ":")
rhsName := strings.Join(rhs.GetFQName(), ":")
entries := policy.GetNetworkPolicyEntries()
for _, rule := range entries.PolicyRule {
if rule.SrcAddresses[0].VirtualNetwork == lhsName &&
rule.DstAddresses[0].VirtualNetwork == rhsName {
return nil
}
}
rule := new(types.PolicyRuleType)
rule.Protocol = "any"
rule.Direction = "<>"
rule.SrcAddresses = []types.AddressType{types.AddressType{
VirtualNetwork: lhsName,
}}
rule.DstAddresses = []types.AddressType{types.AddressType{
VirtualNetwork: rhsName,
}}
rule.SrcPorts = []types.PortType{types.PortType{-1, -1}}
rule.DstPorts = []types.PortType{types.PortType{-1, -1}}
rule.ActionList = &types.ActionListType{
SimpleAction: "pass",
}
entries.AddPolicyRule(rule)
policy.SetNetworkPolicyEntries(&entries)
err := m.client.Update(policy)
if err != nil {
glog.Errorf("policy-rule: %v", err)
return err
}
return nil
}
示例2: DeleteConnections
func (m *NetworkManagerImpl) DeleteConnections(network *types.VirtualNetwork, policies map[string]string) error {
for policyID, _ := range policies {
network.DeleteNetworkPolicy(policyID)
}
err := m.client.Update(network)
for policyID, targetName := range policies {
policy, policyErr := types.NetworkPolicyByUuid(m.client, policyID)
if policyErr != nil {
glog.Error(policyErr)
continue
}
networkRefs, policyErr := policy.GetVirtualNetworkBackRefs()
if len(networkRefs) > 1 {
continue
}
if len(networkRefs) == 1 {
if strings.Join(networkRefs[0].To, ":") == targetName {
policyErr = m.disconnectNetworkFromPolicy(policy, targetName)
if policyErr != nil {
glog.Error(policyErr)
continue
}
} else {
continue
}
}
policyErr = m.client.Delete(policy)
if policyErr != nil {
glog.Error(policyErr)
}
}
return err
}
示例3: DeleteConnections
func (m *ServiceManagerImpl) DeleteConnections(network *types.VirtualNetwork, purgeList []string) error {
if len(purgeList) == 0 {
return nil
}
for _, policyId := range purgeList {
network.DeleteNetworkPolicy(policyId)
}
err := m.client.Update(network)
if err != nil {
return err
}
for _, policyId := range purgeList {
policy, err := types.NetworkPolicyByUuid(m.client, policyId)
if err != nil {
glog.Error(err)
continue
}
refs, err := policy.GetVirtualNetworkBackRefs()
if err != nil {
glog.Error(err)
continue
}
if len(refs) == 0 {
err = m.client.Delete(policy)
if err != nil {
glog.Error(err)
}
}
}
return nil
}
示例4: TestReadChildren
func TestReadChildren(t *testing.T) {
client := new(ApiClient)
client.Init()
project := new(types.Project)
project.SetFQName("domain", []string{"default-domain", "p1"})
assert.NoError(t, client.Create(project))
net := new(types.VirtualNetwork)
net.SetFQName("project", []string{"default-domain", "p1", "n1"})
assert.NoError(t, client.Create(net))
vmi1 := new(types.VirtualMachineInterface)
vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"})
vmi1.AddVirtualNetwork(net)
assert.NoError(t, client.Create(vmi1))
vmi2 := new(types.VirtualMachineInterface)
vmi2.SetFQName("project", []string{"default-domain", "p1", "port2"})
vmi2.AddVirtualNetwork(net)
assert.NoError(t, client.Create(vmi2))
refs, err := project.GetVirtualMachineInterfaces()
assert.NoError(t, err)
assert.Len(t, refs, 2)
}
示例5: globalNetworkCheckPolicyAttachment
func (c *consistencyChecker) globalNetworkCheckPolicyAttachment(network *types.VirtualNetwork) bool {
policyName := makeGlobalNetworkPolicyName(c.config, network.GetFQName())
policy, err := types.NetworkPolicyByName(c.client, strings.Join(policyName, ":"))
if err != nil {
glog.V(3).Infof("No network policy for %s", network.GetName())
return true
}
policyRefs, err := network.GetNetworkPolicyRefs()
if err != nil {
glog.Error(err)
return true
}
for _, ref := range policyRefs {
if ref.Uuid == policy.GetUuid() {
glog.V(5).Infof("Network %s attached to %s", network.GetName(), policy.GetUuid())
return true
}
}
err = policyAttach(c.client, network, policy)
if err != nil {
glog.Error(err)
} else {
glog.Infof("attached global network %s to policy", strings.Join(network.GetFQName(), ":"))
}
return false
}
示例6: LocateInterface
func (m *InstanceManager) LocateInterface(
network *types.VirtualNetwork, instance *types.VirtualMachine) *types.VirtualMachineInterface {
tenant := network.GetFQName()[len(network.GetFQName())-2]
fqn := interfaceFQName(m.config.DefaultDomain, tenant, instance.GetName())
obj, err := m.client.FindByName(
"virtual-machine-interface", strings.Join(fqn, ":"))
if err == nil {
nic := obj.(*types.VirtualMachineInterface)
// TODO(prm): ensure network is as expected, else update.
return nic
}
nic := new(types.VirtualMachineInterface)
nic.SetFQName("project", fqn)
nic.AddVirtualMachine(instance)
if network != nil {
nic.AddVirtualNetwork(network)
}
err = m.client.Create(nic)
if err != nil {
glog.Errorf("Create interface %s: %v", instance.GetName(), err)
return nil
}
obj, err = m.client.FindByUuid(nic.GetType(), nic.GetUuid())
if err != nil {
glog.Errorf("Get vmi %s: %v", nic.GetUuid(), err)
return nil
}
return obj.(*types.VirtualMachineInterface)
}
示例7: TestPropertyUpdate
func TestPropertyUpdate(t *testing.T) {
client := contrail.NewClient("localhost", 8082)
net := types.VirtualNetwork{}
net.SetName("test")
err := client.Create(&net)
if err != nil {
t.Error(err)
}
defer client.Delete(&net)
props := net.GetVirtualNetworkProperties()
if len(props.ForwardingMode) > 0 {
t.Error(props.ForwardingMode)
}
props.ForwardingMode = "l2_l3"
net.SetVirtualNetworkProperties(&props)
err = client.Update(&net)
if err != nil {
t.Error(err)
}
obj, err := client.FindByUuid("virtual-network", net.GetUuid())
if err != nil {
t.Fatal(err)
}
netPtr := obj.(*types.VirtualNetwork)
p2 := netPtr.GetVirtualNetworkProperties()
if p2.ForwardingMode != "l2_l3" {
t.Errorf("Expected: l2_l3 got: %s", p2.ForwardingMode)
}
}
示例8: DeleteFloatingIpPool
func (m *NetworkManagerImpl) DeleteFloatingIpPool(network *types.VirtualNetwork, cascade bool) error {
obj, err := m.client.FindByName("floating-ip-pool", makePoolName(network))
if err != nil {
glog.Errorf("Get floating-ip-pool %s: %v", network.GetName(), err)
return err
}
if cascade {
pool := obj.(*types.FloatingIpPool)
m.floatingIpPoolDeleteChildren(pool)
}
m.client.Delete(obj)
return nil
}
示例9: globalNetworkConnectionUpdate
func (c *Controller) globalNetworkConnectionUpdate(network *types.VirtualNetwork) {
if !networkAccessGlobalNetworks(c.config, network.GetFQName()) {
return
}
// connect to each of the global-networks
for _, gbl := range c.config.GlobalNetworks {
err := c.networkMgr.Connect(network, gbl)
if err != nil {
glog.Error(err)
continue
}
glog.V(2).Infof("Connected %s to %s", strings.Join(network.GetFQName(), ":"), gbl)
}
}
示例10: TestPodCreate
func TestPodCreate(t *testing.T) {
kube := mocks.NewKubeClient()
client := new(contrail_mocks.ApiClient)
client.Init()
client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{})
allocator := new(mocks.AddressAllocator)
networkMgr := new(mocks.NetworkManager)
controller := NewTestController(kube, client, allocator, networkMgr)
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test",
Namespace: "testns",
UID: kubetypes.UID(uuid.New()),
Labels: map[string]string{
"name": "testnet",
},
},
}
netnsProject := new(types.Project)
netnsProject.SetUuid(uuid.New())
netnsProject.SetFQName("", []string{"default-domain", "testns"})
client.Create(netnsProject)
testnet := new(types.VirtualNetwork)
testnet.SetFQName("project", []string{"default-domain", "testns", "testnet"})
client.Create(testnet)
allocator.On("LocateIpAddress", string(pod.ObjectMeta.UID)).Return("10.0.0.42", nil)
networkMgr.On("LocateNetwork", "testns", "testnet",
controller.config.PrivateSubnet).Return(testnet, nil)
networkMgr.On("GetGatewayAddress", testnet).Return("10.0.255.254", nil)
kube.PodInterface.On("Update", pod).Return(pod, nil)
shutdown := make(chan struct{})
go controller.Run(shutdown)
controller.AddPod(pod)
time.Sleep(100 * time.Millisecond)
type shutdownMsg struct {
}
shutdown <- shutdownMsg{}
kube.PodInterface.AssertExpectations(t)
}
示例11: CreateNetworkWithSubnet
func CreateNetworkWithSubnet(
client contrail.ApiClient, project_id, name, prefix string) (
string, error) {
expr := regexp.MustCompile(`(([0-9]{1,3}\.){3}[0-9]{1,3})/([0-9]{1,2})`)
match := expr.FindStringSubmatch(prefix)
if match == nil {
return "", fmt.Errorf("Invalid subnet prefix %s", prefix)
}
address := match[1]
prefixlen, _ := strconv.Atoi(match[3])
obj, err := client.FindByUuid("project", project_id)
if err != nil {
return "", err
}
project := obj.(*types.Project)
refList, err := project.GetNetworkIpams()
if err != nil {
return "", err
}
var ipam *types.NetworkIpam
if len(refList) > 0 {
obj, err := client.FindByUuid("network-ipam", refList[0].Uuid)
if err != nil {
return "", err
}
ipam = obj.(*types.NetworkIpam)
} else {
obj, err := client.FindByName("network-ipam",
"default-domain:default-project:default-network-ipam")
if err != nil {
return "", err
}
ipam = obj.(*types.NetworkIpam)
}
net := new(types.VirtualNetwork)
net.SetParent(project)
net.SetName(name)
subnets := types.VnSubnetsType{}
subnets.AddIpamSubnets(
&types.IpamSubnetType{
Subnet: &types.SubnetType{address, prefixlen}})
net.AddNetworkIpam(ipam, subnets)
err = client.Create(net)
if err != nil {
return "", err
}
return net.GetUuid(), nil
}
示例12: TestCreate
func TestCreate(t *testing.T) {
client := contrail.NewClient("localhost", 8082)
net := types.VirtualNetwork{}
net.SetName("test")
ipam, err := client.FindByName("network-ipam",
"default-domain:default-project:default-network-ipam")
if err != nil {
t.Fatal(err)
}
subnets := types.VnSubnetsType{}
subnets.AddIpamSubnets(
&types.IpamSubnetType{
Subnet: &types.SubnetType{"10.0.0.0", 8}})
net.AddNetworkIpam(ipam.(*types.NetworkIpam), subnets)
err = client.Create(&net)
if err != nil {
t.Error(err)
}
if len(net.GetUuid()) == 0 {
t.Error("No uuid assigned")
}
netPtr, err := client.FindByUuid("virtual-network", net.GetUuid())
if err != nil {
t.Error(err)
} else {
xnet := netPtr.(*types.VirtualNetwork)
refs, err := xnet.GetNetworkIpamRefs()
if err != nil {
t.Error(err)
} else if len(refs) > 0 {
xattr := refs[0].Attr.(types.VnSubnetsType)
if len(xattr.IpamSubnets) == 1 {
isubnet := xattr.IpamSubnets[0]
if isubnet.Subnet.IpPrefix != "10.0.0.0" ||
isubnet.Subnet.IpPrefixLen != 8 {
t.Errorf("Bad subnet %s/%d",
isubnet.Subnet.IpPrefix,
isubnet.Subnet.IpPrefixLen)
}
} else {
t.Errorf("%d subnets", len(xattr.IpamSubnets))
}
} else {
t.Error("Empty network-ipam reference list")
}
}
err = client.Delete(&net)
if err != nil {
t.Error(err)
}
}
示例13: LocateFloatingIpPool
func (m *NetworkManagerImpl) LocateFloatingIpPool(network *types.VirtualNetwork) (*types.FloatingIpPool, error) {
obj, err := m.client.FindByName("floating-ip-pool", makePoolName(network))
if err == nil {
return obj.(*types.FloatingIpPool), nil
}
pool := new(types.FloatingIpPool)
pool.SetName(network.GetName())
pool.SetParent(network)
err = m.client.Create(pool)
if err != nil {
glog.Errorf("Create floating-ip-pool %s: %v", network.GetName(), err)
return nil, err
}
return pool, nil
}
示例14: PurgeStalePolicyRefs
func (m *ServiceManagerImpl) PurgeStalePolicyRefs(network *types.VirtualNetwork, services ServiceIdList,
doDelete func(string, string) bool) error {
purgeList := make([]string, 0)
refs, err := network.GetNetworkPolicyRefs()
if err != nil {
return err
}
for _, ref := range refs {
if len(ref.To) < 3 {
glog.Errorf("unexpected policy id %+v", ref.To)
continue
}
namespace := ref.To[1]
serviceName := ref.To[len(ref.To)-1]
if !services.Contains(namespace, serviceName) && doDelete(namespace, serviceName) {
purgeList = append(purgeList, ref.Uuid)
}
}
if len(purgeList) == 0 {
return nil
}
for _, policyId := range purgeList {
network.DeleteNetworkPolicy(policyId)
}
err = m.client.Update(network)
if err != nil {
return err
}
for _, policyId := range purgeList {
policy, err := types.NetworkPolicyByUuid(m.client, policyId)
if err != nil {
glog.Error(err)
continue
}
refs, err := policy.GetVirtualNetworkBackRefs()
if err != nil {
glog.Error(err)
}
if len(refs) == 0 {
err = m.client.Delete(policy)
if err != nil {
glog.Error(err)
}
}
}
return nil
}
示例15: NetworkIPPrefixList
//
// Retrieve the list of IP prefixes associated with this network.
//
func (m *NetworkManagerImpl) NetworkIPPrefixList(network *types.VirtualNetwork) ([]string, error) {
prefixList := make([]string, 0)
refList, err := network.GetNetworkIpamRefs()
if err != nil {
return nil, err
}
for _, ref := range refList {
attr := ref.Attr.(types.VnSubnetsType)
for _, ipamSubnet := range attr.IpamSubnets {
prefixList = append(prefixList,
fmt.Sprintf("%s/%d", ipamSubnet.Subnet.IpPrefix, ipamSubnet.Subnet.IpPrefixLen))
}
}
return prefixList, nil
}