예제 #1
0
func main() {
	config := LoadConfig()

	hostConfig := ibclient.HostConfig{
		Host:     config.GridHost,
		Version:  config.WapiVer,
		Port:     config.WapiPort,
		Username: config.WapiUsername,
		Password: config.WapiPassword,
	}

	transportConfig := ibclient.NewTransportConfig(
		config.SslVerify,
		config.HttpRequestTimeout,
		config.HttpPoolConnections,
	)

	requestBuilder := &ibclient.WapiRequestBuilder{}
	requestor := &ibclient.WapiHttpRequestor{}

	conn, err := ibclient.NewConnector(hostConfig, transportConfig, requestBuilder, requestor)

	if err != nil {
		log.Fatal(err)
	}

	objMgr := ibclient.NewObjectManager(conn, "Docker", "")

	reqEaDefs := GetRequiredEADefs()
	for _, e := range reqEaDefs {
		eadef, err := objMgr.GetEADefinition(e.Name)

		if err != nil {
			log.Printf("GetEADefinition(%s) error '%s'", e.Name, err)
			continue
		}

		if eadef != nil {
			log.Printf("EA Definition '%s' already exists", eadef.Name)

		} else {
			log.Printf("EA Definition '%s' not found.", e.Name)
			newEadef, err := objMgr.CreateEADefinition(e)
			if err == nil {
				log.Printf("EA Definition '%s' created", newEadef.Name)
			}
		}
	}
}
예제 #2
0
func main() {
	config := LoadConfig()

	conn, err := ibclient.NewConnector(
		config.GridHost,
		config.WapiVer,
		config.WapiPort,
		config.WapiUsername,
		config.WapiPassword,
		config.SslVerify,
		config.HttpRequestTimeout,
		config.HttpPoolConnections,
		config.HttpPoolMaxSize)

	if err != nil {
		log.Fatal(err)
	}

	objMgr := ibclient.NewObjectManager(conn, "")

	reqEaDefs := GetRequiredEADefs()
	for _, e := range reqEaDefs {
		eadef, err := objMgr.GetEADefinition(e.Name)

		if err != nil {
			log.Printf("GetEADefinition(%s) error '%s'", e.Name, err)
			continue
		}

		if eadef != nil {
			log.Printf("EA Definition '%s' already exists", eadef.Name)

		} else {
			log.Printf("EA Definition '%s' not found.", e.Name)
			newEadef, err := objMgr.CreateEADefinition(e)
			if err == nil {
				log.Printf("EA Definition '%s' created", newEadef.Name)
			}
		}
	}
}
예제 #3
0
func main() {
	config := LoadConfig()

	socketFile := setupSocket(config.PluginDir, config.DriverName)
	log.Printf("Driver Name: '%s'", config.DriverName)
	log.Printf("Socket File: '%s'", socketFile)

	conn, err := ibclient.NewConnector(
		config.GridHost,
		config.WapiVer,
		config.WapiPort,
		config.WapiUsername,
		config.WapiPassword,
		config.SslVerify,
		config.HttpRequestTimeout,
		config.HttpPoolConnections,
		config.HttpPoolMaxSize)

	if err != nil {
		log.Fatal(err)
	}

	dockerID, _ := getDockerID()
	if len(dockerID) > 0 {
		log.Printf("Docker id is '%s'\n", dockerID)
	}
	objMgr := ibclient.NewObjectManager(conn, dockerID)

	ipamDrv := NewInfobloxDriver(objMgr, config.GlobalNetview, config.GlobalNetworkContainer, config.GlobalPrefixLength,
		config.LocalNetview, config.LocalNetworkContainer, config.LocalPrefixLength)
	ipamCalls := []ipamCall{
		{"/Plugin.Activate", ipamDrv.PluginActivate, nil},
		{"/IpamDriver.GetCapabilities", ipamDrv.GetCapabilities, nil},
		{"/IpamDriver.GetDefaultAddressSpaces", ipamDrv.GetDefaultAddressSpaces, nil},
		{"/IpamDriver.RequestPool", ipamDrv.RequestPool,
			reflect.TypeOf(ipamsapi.RequestPoolRequest{})},
		{"/IpamDriver.ReleasePool", ipamDrv.ReleasePool,
			reflect.TypeOf(ipamsapi.ReleasePoolRequest{})},
		{"/IpamDriver.RequestAddress", ipamDrv.RequestAddress,
			reflect.TypeOf(ipamsapi.RequestAddressRequest{})},
		{"/IpamDriver.ReleaseAddress", ipamDrv.ReleaseAddress,
			reflect.TypeOf(ipamsapi.ReleaseAddressRequest{})},
	}

	handlers := make(map[string]ipamCall)

	for _, v := range ipamCalls {
		handlers[v.url] = v
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		url := r.URL.String()
		log.Printf("Plugin: %s\n", url)
		if c, ok := handlers[url]; ok {

			//var req interface{}
			var req interface{} = nil
			if c.t != nil {
				req = reflect.New(c.t).Interface()
				if err := json.NewDecoder(r.Body).Decode(req); err != nil {
					log.Printf("%s: Bad Request Error: %s\n", url, err)
					http.Error(w, err.Error(), http.StatusBadRequest)
					return
				}
			}

			res, err := c.f(req)
			if err != nil || res == nil {
				if err != nil {
					log.Printf("IPAM Driver error '%s'", err)
				} else if res == nil {
					log.Printf("IPAM Driver returned nil result")
				}
				http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
			} else {
				if err := json.NewEncoder(w).Encode(res); err != nil {
					log.Printf("%s: Bad Response Error: %s\n", url, err)
				}
			}
		}
		fmt.Fprintf(w, "{ \"Error\": \"%s\"}", url)
	})

	l, err := net.Listen("unix", socketFile)
	if err != nil {
		log.Panic(err)
	}
	if err := http.Serve(l, nil); err != nil {
		log.Panic(err)
	}

	os.Exit(0)
}