func loadConfig(filePath string) (ctx *cpi.Context, err error) {
	file, err := os.Open(filePath)
	if err != nil {
		return
	}
	config := &cpi.Config{}
	err = json.NewDecoder(file).Decode(config)
	if err != nil {
		return
	}

	token, err := getToken(config.Photon)
	if err != nil {
		return
	}

	tokenOptions := &photon.TokenOptions{
		AccessToken: token}
	clientConfig := &photon.ClientOptions{
		IgnoreCertificate: config.Photon.IgnoreCertificate,
		TokenOptions:      tokenOptions,
	}
	ctx = &cpi.Context{
		Client: photon.NewClient(config.Photon.Target, clientConfig, nil),
		Config: config,
		Runner: cmd.NewRunner(),
		Logger: logger.New(),
	}
	return
}
예제 #2
0
func NewClient(config *cf.Configuration) (*photon.Client, error) {
	if len(config.CloudTarget) == 0 {
		return nil, errors.New("Specify a Photon Controller endpoint by running 'target set' command")
	}

	options := &photon.ClientOptions{
		IgnoreCertificate: config.IgnoreCertificate,
		TokenOptions: &photon.TokenOptions{
			AccessToken: config.Token,
		},
	}

	//
	// If target is https, check if we could ignore client side cert check
	// If we can't ignore client side cert check, try setting the root certs
	//
	u, err := url.Parse(config.CloudTarget)
	if err == nil && u.Scheme == "https" {
		if !config.IgnoreCertificate == true {
			roots, err := cf.GetCertsFromLocalStore()
			if err == nil {
				options.RootCAs = roots
			} else {
				return nil, err
			}
		}
	}

	esxclient := photon.NewClient(config.CloudTarget, options, logger)
	return esxclient, nil
}
예제 #3
0
func newPCCloud(cfg PCConfig) (*PCCloud, error) {
	if len(cfg.Global.CloudTarget) == 0 {
		return nil, fmt.Errorf("Photon Controller endpoint was not specified.")
	}

	// Currently we support Photon Controller endpoint with authentication disabled.
	options := &photon.ClientOptions{
		IgnoreCertificate: cfg.Global.IgnoreCertificate,
	}

	photonClient = photon.NewClient(cfg.Global.CloudTarget, options, logger)
	status, err := photonClient.Status.Get()
	if err != nil {
		glog.Errorf("Photon Cloud Provider: new client creation failed. Error[%v]", err)
		return nil, err
	}
	glog.V(2).Info("Photon Cloud Provider: Status of the new photon controller client: %v", status)

	// Get Photon Controller project ID for future use
	projID, err := getProjIDbyName(cfg.Global.Tenant, cfg.Global.Project)
	if err != nil {
		glog.Errorf("Photon Cloud Provider: getProjIDbyName failed when creating new Photon Controller client. Error[%v]", err)
		return nil, err
	}

	// Get local hostname for localInstanceID
	cmd := exec.Command("bash", "-c", `echo $HOSTNAME`)
	out, err := cmd.CombinedOutput()
	if err != nil {
		glog.Errorf("Photon Cloud Provider: get local hostname bash command failed. Error[%v]", err)
		return nil, err
	}
	if len(out) == 0 {
		glog.Errorf("unable to retrieve hostname for Instance ID")
		return nil, fmt.Errorf("unable to retrieve hostname for Instance ID")
	}
	hostname := strings.TrimRight(string(out), "\n")
	vmID, err := getVMIDbyNodename(projID, hostname)
	if err != nil {
		glog.Errorf("Photon Cloud Provider: getVMIDbyNodename failed when creating new Photon Controller client. Error[%v]", err)
		return nil, err
	}

	pc := PCCloud{
		cfg:              &cfg,
		localInstanceID:  vmID,
		localHostname:    hostname,
		localK8sHostname: "",
		projID:           projID,
	}

	overrideIP = cfg.Global.OverrideIP

	return &pc, nil
}
func getToken(photonConfig *cpi.PhotonConfig) (token string, err error) {
	if len(photonConfig.Username) == 0 && len(photonConfig.Password) == 0 {
		return
	}

	if len(photonConfig.Username) == 0 || len(photonConfig.Password) == 0 {
		err = errors.New("Must provide both username and password. One is missing.")
		return
	}

	clientConfig := &photon.ClientOptions{
		IgnoreCertificate: photonConfig.IgnoreCertificate,
	}

	client := photon.NewClient(photonConfig.Target, clientConfig, nil)

	options, err := client.Auth.GetTokensByPassword(photonConfig.Username, photonConfig.Password)
	if err != nil {
		return
	}

	token = options.AccessToken
	return
}
}

var _ = BeforeSuite(func() {
	// Create JSON config file used by CPI. Agent config is unused for this test suite.
	configJson := `{
	"photon": {
		"target": "%s",
		"project": "%s"
	},
	"agent": {
		"mbus": "nats://*****:*****@127.0.0.1",
		"ntp": ["127.0.0.1"]
	}
}`
	target := os.Getenv("TEST_TARGET")
	client = ec.NewClient(target, nil, nil)
	fmt.Printf("Target: %s\n", target)

	err := ECSetup()
	if err != nil {
		panic(err)
	}

	configJson = fmt.Sprintf(configJson, target, project, tenant)
	configFile, err := ioutil.TempFile("", "bosh-cpi-config")
	if err != nil {
		panic(err)
	}
	defer configFile.Close()

	_, err = configFile.WriteString(configJson)