// Returns the current configuration of this Jenkins node from the Jenkins server.
func GetJenkinsNodeConfig(config *util.Config) (*JenkinsNodeConfig, error) {
	if response, err := config.CIGet(fmt.Sprintf("/computer/%s/config.xml", config.ClientName)); err == nil {
		defer response.Body.Close()
		if response.StatusCode == 200 {
			config := &JenkinsNodeConfig{}
			err = xml.NewDecoder(response.Body).Decode(config)
			return config, err
		} else {
			return nil, fmt.Errorf(response.Status)
		}
	} else {
		return nil, err
	}
}
// Returns the names of all nodes that are registered in Jenkins.
func GetAllRegisteredNodesInJenkins(config *util.Config) (*AllComputerNames, error) {
	response, err := config.CIGet(ComputersURI)
	if err == nil && response.StatusCode == 200 {
		defer response.Body.Close()
		names := new(AllComputerNames)
		err = xml.NewDecoder(response.Body).Decode(names)
		return names, err
	} else {
		if err == nil && response != nil {
			err = fmt.Errorf(response.Status)
		}
		return nil, err
	}
}
// Returns the current offline and idle status of this Jenkins node from the Jenkins server.
func GetJenkinsNodeStatus(config *util.Config) (*JenkinsNodeStatus, error) {
	if response, err := config.CIGet(fmt.Sprintf(NodeMonitoringURI, config.ClientName)); err == nil {
		defer response.Body.Close()
		if response.StatusCode == 200 {
			status := &JenkinsNodeStatus{}
			err = xml.NewDecoder(response.Body).Decode(status)
			return status, err
		} else {
			return nil, fmt.Errorf(response.Status)
		}
	} else {
		return nil, err
	}
}
// Gets the port that is used by the JNLP client to communicate with Jenkins.
// Returns the port number as string and an error if fetching the port failed for any reason.
func (self *SSHTunnelEstablisher) getJNLPListenerPort(config *util.Config) (port string, err error) {
	var response *http.Response
	port = ""

	if response, err = config.CIGet("/tcpSlaveAgentListener/"); err == nil {
		response.Body.Close()
		if response.StatusCode == 200 {
			if port = response.Header.Get("X-Jenkins-JNLP-Port"); port == "" {
				port = response.Header.Get("X-Hudson-JNLP-Port")
			}
		}

		if port == "" {
			err = fmt.Errorf("Jenkins did not provide the JNLP-Port, the reply was %v.", response.Status)
		}
	}

	return
}
func (self *ClientMode) getCustomizedAgentJnlp(config *util.Config) []byte {
	response, err := config.CIGet(fmt.Sprintf("computer/%s/slave-agent.jnlp", config.ClientName))
	if err == nil {
		defer response.Body.Close()

		if response.StatusCode == 200 {
			var content []byte
			if content, err = ioutil.ReadAll(response.Body); err == nil {
				return self.applyCustomJnlpArgs(config, content)
			}
		} else {
			util.GOut("client", "ERROR: Failed JNLP config from Jenkins. Cause: %v", response.Status)
		}
	}

	if err != nil {
		util.GOut("client", "ERROR: Failed JNLP config from Jenkins. Cause: %v", err)
	}

	return nil
}
func (self *ClientMode) getSecretFromJenkins(config *util.Config) string {
	response, err := config.CIGet(fmt.Sprintf("computer/%s/", config.ClientName))
	if err == nil {
		defer response.Body.Close()

		if response.StatusCode == 200 {
			var content []byte
			if content, err = ioutil.ReadAll(response.Body); err == nil {
				return self.extractSecret(content)
			}
		} else {
			util.GOut("client", "ERROR: Failed fetching secret key from Jenkins. Cause: %v", response.Status)
		}
	}

	if err != nil {
		util.GOut("client", "ERROR: Failed fetching secret key from Jenkins. Cause: %v", err)
	}

	return ""
}
func (self *NodeNameHandler) createNode(config *util.Config) error {
	toJson := func(v interface{}) string {
		if content, err := json.Marshal(v); err == nil {
			return string(content)
		} else {
			return fmt.Sprintf("{\"error\": \"%v\"}", err)
		}
	}

	cwd, _ := os.Getwd()
	mode := "EXCLUSIVE"                                   // NORMAL or EXCLUSIVE (tied jobs only)
	retention := "hudson.slaves.RetentionStrategy$Always" // Always on

	params := make(url.Values)
	params.Set("name", config.ClientName)
	params.Set("type", ExpectedNodeType)
	params.Set("json", toJson(map[string]interface{}{
		"name":              config.ClientName,
		"nodeDescription":   fmt.Sprintf("JSL auto generated node '%s'.", config.ClientName),
		"numExecutors":      1,
		"remoteFS":          cwd,
		"labelString":       fmt.Sprintf("JSL %s %s", runtime.GOOS, runtime.GOARCH),
		"mode":              mode,
		"type":              ExpectedNodeType,
		"retentionStrategy": map[string]interface{}{"stapler-class": retention},
		"nodeProperties":    map[string]interface{}{"stapler-class-bag": true},
		"launcher":          map[string]interface{}{"stapler-class": ExpectedNodeLauncher},
	}))

	if response, err := config.CIGet(CreateNodeURI + "?" + params.Encode()); err == nil {
		response.Body.Close()
		if response.StatusCode == 200 {
			return nil
		} else {
			return fmt.Errorf("Create node failed. Jenkins returned %v", response.Status)
		}
	} else {
		return err
	}
}