mrutkows commented on a change in pull request #392: Load the credentials the 
same way as client go does
URL: 
https://github.com/apache/incubator-openwhisk-wskdeploy/pull/392#discussion_r135302107
 
 

 ##########
 File path: deployers/whiskclient.go
 ##########
 @@ -28,113 +28,165 @@ import (
        "github.com/apache/incubator-openwhisk-client-go/whisk"
        "github.com/apache/incubator-openwhisk-wskdeploy/parsers"
        "github.com/apache/incubator-openwhisk-wskdeploy/utils"
+    "errors"
 )
 
+const (
+    DEPLOYMENTFILE = "deployment.yml"
+    COMMANDLINE = "wskdeploy command line"
+    DEFAULTVALUE = "default value"
+    WSKPROPS = ".wskprops"
+    WHISKPROPERTY = "whisk.properties"
+    INTERINPUT = "interactve input"
+)
+
+type PropertyValue struct {
+    Value string
+    Source string
+}
+
+var GetPropertyValue = func (prop PropertyValue, newValue string, source 
string) PropertyValue {
+    if len(prop.Value) == 0 && len(newValue) > 0 {
+        prop.Value = newValue
+        prop.Source = source
+    }
+    return prop
+}
+
+var GetPropertyUrlValueFromHost = func (prop PropertyValue, newValue string, 
source string) PropertyValue {
+    if len(prop.Value) == 0 && len(newValue) > 0 {
+        url, error := utils.GetURLBase(newValue)
+        // If the api host is set in a correct format, convert it into base 
url.
+        if error == nil {
+            prop.Value = url.String()
+            prop.Source = COMMANDLINE
+        }
+    }
+    return prop
+}
+
+var GetWhiskPropertiesImp = func () whisk.PropertiesImp {
+    return whisk.GetPropertiesImp()
+}
+
+var GetWskPropFromWskprops = func (pi whisk.Properties, proppath string) 
(*whisk.Wskprops, error) {
+    return whisk.GetWskPropFromWskprops(pi, proppath)
+}
+
+var GetWskPropFromWhiskProperty = func (pi whisk.Properties) (*whisk.Wskprops, 
error) {
+    return whisk.GetWskPropFromWhiskProperty(pi)
+}
+
+var GetCommandLineFlags = func () (string, string, string) {
+    return utils.Flags.ApiHost, utils.Flags.Auth, utils.Flags.Namespace
+}
+
 func NewWhiskClient(proppath string, deploymentPath string, isInteractive 
bool) (*whisk.Client, *whisk.Config) {
-       var clientConfig *whisk.Config
-
-       configs, err := utils.LoadConfiguration(proppath)
-       utils.Check(err)
-
-       credential := configs[2]
-       if len(utils.Flags.Auth) > 0 {
-               credential = utils.Flags.Auth
-       }
-       namespace := configs[0]
-
-       if namespace == "" {
-               namespace = "_"
-       }
-       //we need to get Apihost from property file which currently not defined 
in sample deployment file.
-
-       u := configs[1]
-       if len(utils.Flags.ApiHost) > 0 {
-               u = utils.Flags.ApiHost
-       }
-
-       var baseURL *url.URL
-
-       if u == "" && isInteractive == true {
-               host, err := promptForValue("\nPlease provide the hostname for 
OpenWhisk [openwhisk.ng.bluemix.net]: ")
-               utils.Check(err)
-               if host == "" {
-                       host = "openwhisk.ng.bluemix.net"
-               }
-
-               fmt.Println("Host set to " + host)
-
-               baseURL, err = utils.GetURLBase(host)
-               utils.Check(err)
-
-       } else if u == "" {
-               // handle some error
-       } else {
-               baseURL, err = utils.GetURLBase(u)
-               utils.Check(err)
-       }
-
-       if utils.FileExists(deploymentPath) {
-               mm := parsers.NewYAMLParser()
-               deployment := mm.ParseDeployment(deploymentPath)
-               // We get the first package from the sample deployment file.
-               credentialDep := deployment.Application.Credential
-               namespaceDep := deployment.Application.Namespace
-               baseUrlDep := deployment.Application.BaseUrl
-
-               if credentialDep != "" {
-                       credential = credentialDep
-               }
-
-               if namespaceDep != "" {
-                       namespace = namespaceDep
-               }
-
-               if baseUrlDep != "" {
-                       u, err := url.Parse(baseUrlDep)
-                       utils.Check(err)
-
-                       baseURL = u
-               }
-
-       }
-
-       if credential == "" && isInteractive == true {
-               cred, err := promptForValue("\nPlease provide an authentication 
token: ")
-               utils.Check(err)
-               credential = cred
-
-               fmt.Println("Authentication token set.")
-       }
-
-       if namespace == "" && isInteractive == true {
-               ns, err := promptForValue("\nPlease provide a namespace 
[default]: ")
-               utils.Check(err)
-
-               if ns == "" {
-                       ns = "_"
-               }
-
-               namespace = ns
-               fmt.Println("Namespace set to '" + namespace + "'")
-       }
-
-       clientConfig = &whisk.Config{
-               AuthToken: credential, //Authtoken
-               Namespace: namespace,  //Namespace
-               BaseURL:   baseURL,
-               Version:   "v1",
-               Insecure:  true, // true if you want to ignore certificate 
signing
-
-       }
-
-       // Setup network client
-       client, err := whisk.NewClient(http.DefaultClient, clientConfig)
-       utils.Check(err)
-       return client, clientConfig
+    credential := PropertyValue {}
+    namespace := PropertyValue {}
+    baseUrl := PropertyValue {}
+
+    // First, we look up the above variables in the deployment file.
+    if utils.FileExists(deploymentPath) {
+        mm := parsers.NewYAMLParser()
+        deployment := mm.ParseDeployment(deploymentPath)
+        credential.Value = deployment.Application.Credential
+        credential.Source = DEPLOYMENTFILE
+        namespace.Value = deployment.Application.Namespace
+        namespace.Source = DEPLOYMENTFILE
+        baseUrl.Value = deployment.Application.BaseUrl
+        baseUrl.Source = DEPLOYMENTFILE
+    }
+
+    // If the variables are not correctly assigned, we look up auth key and 
api host in the command line. The namespace
+    // is currently not available in command line, which can be added later.
+    apiHost, auth, ns := GetCommandLineFlags()
+    credential = GetPropertyValue(credential, auth, COMMANDLINE)
+    namespace = GetPropertyValue(namespace, ns, COMMANDLINE)
+    baseUrl = GetPropertyUrlValueFromHost(baseUrl, apiHost, COMMANDLINE)
+
+    // Third, we need to look up the variables in .wskprops file.
+    pi := GetWhiskPropertiesImp()
+    wskprops, _ := GetWskPropFromWskprops(pi, proppath)
+    credential = GetPropertyValue(credential, wskprops.AuthKey, WSKPROPS)
+    namespace = GetPropertyValue(namespace, wskprops.Namespace, WSKPROPS)
+    baseUrl = GetPropertyValue(baseUrl, wskprops.WHISKAPIURL, WSKPROPS)
+
+    // Fourth, we look up the variables in whisk.properties on a local 
openwhisk deployment.
+    whiskproperty, _ := GetWskPropFromWhiskProperty(pi)
+    credential = GetPropertyValue(credential, whiskproperty.AuthKey, 
WHISKPROPERTY)
+    namespace = GetPropertyValue(namespace, whiskproperty.Namespace, 
WHISKPROPERTY)
+    baseUrl = GetPropertyValue(baseUrl, whiskproperty.WHISKAPIURL, 
WHISKPROPERTY)
+
+    // If we still can not find the variables we need, check if it is 
interactive mode. If so, we accept the input
+    // from the user. The namespace will be set to a default value, when the 
code reaches this line, because WSKPROPS
+    // has a default value for namespace.
+    if len(baseUrl.Value) == 0 && isInteractive == true {
 
 Review comment:
   @houshengbo Nice catch, forgot about interactive mode.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to