Example #1
0
func netdcliAdd(payload *intent.Config) error {
	c := client.New("netmaster:9999")
	log.Infof("netdcliAdd payload: %+v", payload)
	if err := c.PostAddConfig(payload); err != nil {
		println(err)
		return err
	}

	return nil
}
Example #2
0
func netdcliAdd(payload *intent.Config) error {
	c := client.New("localhost:9999")
	fmt.Println(payload)
	if err := c.PostAddConfig(payload); err != nil {
		println(err)
		return err
	}

	return nil
}
Example #3
0
func netdcliGetNetwork(name string) ([]drivers.OvsCfgNetworkState, error) {
	var network []drivers.OvsCfgNetworkState

	c := client.New("netmaster:9999")
	content, err := c.GetNetwork(name)
	if err != nil {
		return network, err
	}

	if err := json.Unmarshal(content, &network); err != nil {
		return network, err
	}

	return network, nil
}
Example #4
0
func netdcliGetEndpoint(name string) ([]drivers.OvsOperEndpointState, error) {
	c := client.New("netmaster:9999")
	content, err := c.GetEndpoint(name)
	if err != nil {
		return nil, err
	}

	var endpoint []drivers.OvsOperEndpointState

	if err := json.Unmarshal(content, &endpoint); err != nil {
		return nil, err
	}

	return endpoint, nil
}
Example #5
0
func netdcliGetNetwork(name string) ([]mastercfg.CfgNetworkState, error) {
	var network []mastercfg.CfgNetworkState

	c := client.New("localhost:9999")
	content, err := c.GetNetwork(name)
	if err != nil {
		return network, err
	}

	if err := json.Unmarshal(content, &network); err != nil {
		return network, err
	}

	return network, nil
}
Example #6
0
func executeJSONCfg(opts *cliOpts) error {
	var (
		err  error
		data []byte
	)

	if opts.idStr == "-" {
		reader := bufio.NewReader(os.Stdin)
		if data, err = ioutil.ReadAll(reader); err != nil {
			return err
		}

	} else {
		if data, err = ioutil.ReadFile(opts.idStr); err != nil {
			return err
		}
	}

	allCfg := &intent.Config{}
	if err = json.Unmarshal(data, allCfg); err != nil {
		log.Errorf("error '%s' unmarshaling tenant cfg, data %s \n", err, data)
		return err
	}
	log.Debugf("parsed config %v \n", allCfg)

	nmc := client.New(opts.netmasterURL)
	switch {
	case opts.cfgDesired:
		err = nmc.PostDesiredConfig(allCfg)
	case opts.cfgAdditions:
		err = nmc.PostAddConfig(allCfg)
	case opts.cfgDeletions:
		err = nmc.PostDeleteConfig(allCfg)
	case opts.cfgHostBindings:
		err = nmc.PostHostBindings(allCfg)
	}

	if err != nil {
		return core.Errorf("error processing cfg. Error: %s", err)
	}

	return nil
}
Example #7
0
func netdcliDel(payload *intent.Config) error {
	c := client.New("netmaster:9999")
	return c.PostDeleteConfig(payload)
}
Example #8
0
func executeOpts(opts *cliOpts) error {
	var coreState core.State

	err := validateOpts(opts)
	if err != nil {
		return err
	}

	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}

	switch opts.construct.Get() {
	case cliConstructEndpoint:
		if opts.oper.Get() == cliOperGet {
			// noop
		} else if opts.oper.Get() == cliOperAttach || opts.oper.Get() == cliOperDetach {
			epCfg := &drivers.OvsCfgEndpointState{}
			epCfg.StateDriver = stateDriver
			err = epCfg.Read(opts.idStr)
			if err != nil {
				log.Errorf("Failed to read ep %s. Error: %s", opts.construct.Get(), err)
				return err
			}
			log.Debugf("read ep state as %v for container %s", epCfg, opts.contName)
			if opts.oper.Get() == cliOperAttach {
				epCfg.ContName = opts.contName
				epCfg.AttachUUID = opts.attachUUID
			} else {
				if epCfg.ContName != opts.contName {
					return core.Errorf("Can not detach container '%s' from endpoint '%s' - "+
						"container not attached", opts.contName, opts.idStr)
				}
				epCfg.ContName = ""
			}
			coreState = epCfg
		} else {
			epCfg := &drivers.OvsCfgEndpointState{}
			epCfg.StateDriver = stateDriver
			epCfg.ID = opts.idStr
			epCfg.NetID = opts.netID
			epCfg.IPAddress = opts.ipAddr
			epCfg.ContName = opts.contName
			epCfg.AttachUUID = opts.attachUUID
			epCfg.HomingHost = opts.homingHost
			epCfg.VtepIP = opts.vtepIP
			epCfg.IntfName = opts.intfName
			coreState = epCfg
		}
	case cliConstructNetwork:
		if opts.oper.Get() == cliOperGet {
			// noop
		} else {
			nwCfg := &drivers.OvsCfgNetworkState{}
			nwCfg.StateDriver = stateDriver
			nwCfg.PktTag, _ = strconv.Atoi(opts.pktTag)
			nwCfg.Tenant = opts.tenant
			nwCfg.PktTagType = opts.pktTagType
			nwCfg.SubnetIP = opts.subnetIP
			nwCfg.SubnetLen = opts.subnetLen
			nwCfg.DefaultGw = opts.defaultGw
			nwCfg.ID = opts.idStr
			coreState = nwCfg
		}
	case cliConstructGlobal:
		gcfg := &gstate.Cfg{}
		gcfg.StateDriver = stateDriver
		if opts.oper.Get() == cliOperGet {
			err = gcfg.Read(opts.tenant)
			log.Debugf("State: %v \n", gcfg)
		} else if opts.oper.Get() == cliOperDelete {
			gcfg.Version = gstate.VersionBeta1
			gcfg.Tenant = opts.tenant
			err = gcfg.Clear()
			if err != nil {
				log.Errorf("Failed to delete %s. Error: %s", opts.construct.Get(), err)
				return err
			}
		} else {
			gcfg.Version = gstate.VersionBeta1
			gcfg.Tenant = opts.tenant
			gcfg.Deploy.DefaultNetType = opts.pktTagType
			gcfg.Auto.SubnetPool = opts.subnetIP
			gcfg.Auto.SubnetLen = opts.subnetLen
			gcfg.Auto.VLANs = opts.vlans
			gcfg.Auto.VXLANs = opts.vxlans
			gcfg.Auto.AllocSubnetLen = opts.allocSubnetLen
			err = gcfg.Write()
		}
		if err != nil {
			return err
		}
		return err
	case cliConstructVLANResource:
		fallthrough
	case cliConstructVXLANResource:
		fallthrough
	case cliConstructSubnetResource:
		if opts.oper.Get() == cliOperGet {
			if cliConstructVLANResource == opts.construct.Get() {
				rsrc := &resources.AutoVLANCfgResource{}
				rsrc.StateDriver = stateDriver
				coreState = rsrc
			}
			if cliConstructVXLANResource == opts.construct.Get() {
				rsrc := &resources.AutoVXLANCfgResource{}
				rsrc.StateDriver = stateDriver
				coreState = rsrc
			}
			if cliConstructSubnetResource == opts.construct.Get() {
				rsrc := &resources.AutoSubnetCfgResource{}
				rsrc.StateDriver = stateDriver
				coreState = rsrc
			}
		} else {
			return core.Errorf("Only get operation is supported for resources")
		}
	}

	switch opts.oper.Get() {
	case cliOperGet:
		var (
			resp    []byte
			content bytes.Buffer
		)
		nmc := client.New(opts.netmasterURL)
		switch opts.construct.Get() {
		case cliConstructNetwork:
			if resp, err = nmc.GetNetwork(opts.idStr); err != nil {
				return core.Errorf("Failed to read %s. Error: %s", opts.construct.Get(), err)
			}
		case cliConstructEndpoint:
			if resp, err = nmc.GetEndpoint(opts.idStr); err != nil {
				return core.Errorf("Failed to read %s. Error: %s", opts.construct.Get(), err)
			}
		case cliConstructVLANResource:
			fallthrough
		case cliConstructVXLANResource:
			fallthrough
		case cliConstructSubnetResource:
			if err = coreState.Read(opts.idStr); err != nil {
				return core.Errorf("Failed to read %s. Error: %s", opts.construct.Get(), err)
			}
			if resp, err = json.Marshal(coreState); err != nil {
				return core.Errorf("Failed to marshal state for %s . Error: %s", opts.construct.Get(), err)
			}
		}

		err := json.Indent(&content, resp, "", "  ")
		if err != nil {
			log.Fatalf("Failed to marshal state %s", resp)
		}
		content.WriteTo(os.Stdout)
	case cliOperAttach, cliOperDetach, cliOperCreate:
		err = coreState.Write()
		if err != nil {
			log.Errorf("Failed to create %s. Error: %s", opts.construct.Get(), err)
			return err
		}
	case cliOperDelete:
		err = coreState.Clear()
		if err != nil {
			log.Errorf("Failed to delete %s. Error: %s", opts.construct.Get(), err)
			return err
		}
	}

	return nil
}