예제 #1
0
파일: http.go 프로젝트: Zordrak/terraform
// createAzureRequest packages up the request with the correct set of headers and returns
// the request object or an error.
func (client client) createAzureRequest(url string, requestType string, contentType string, data []byte) (*http.Request, error) {
	var request *http.Request
	var err error

	if data != nil {
		body := bytes.NewBuffer(data)
		request, err = http.NewRequest(requestType, url, body)
	} else {
		request, err = http.NewRequest(requestType, url, nil)
	}

	if err != nil {
		return nil, err
	}

	request.Header.Set(msVersionHeader, client.config.APIVersion)
	request.Header.Set(uaHeader, client.config.UserAgent)

	if contentType != "" {
		request.Header.Set(contentHeader, contentType)
	} else {
		request.Header.Set(contentHeader, defaultContentHeaderValue)
	}

	return request, nil
}
예제 #2
0
// createAzureRequest packages up the request with the correct set of headers and returns
// the request object or an error.
func (client client) createAzureRequest(url string, requestType string, contentType string, data []byte) (*http.Request, error) {
	var request *http.Request
	var err error

	url = fmt.Sprintf("%s/%s/%s", client.config.ManagementURL, client.publishSettings.SubscriptionID, url)
	if data != nil {
		body := bytes.NewBuffer(data)
		request, err = http.NewRequest(requestType, url, body)
	} else {
		request, err = http.NewRequest(requestType, url, nil)
	}

	if err != nil {
		return nil, err
	}

	request.Header.Set(msVersionHeader, client.config.APIVersion)
	request.Header.Set(uaHeader, client.config.UserAgent)

	if contentType != "" {
		request.Header.Set(contentHeader, contentType)
	} else {
		request.Header.Set(contentHeader, defaultContentHeaderValue)
	}

	return request, nil
}
예제 #3
0
// Do sends a request.
func (c *client) Do(req *http.Request) (*http.Response, error) {
	if req.URL == "" {
		return nil, ErrURLNotSpecified
	}

	httpReq, err := azhttp.NewRequest(req.Method, req.URL, bytes.NewBuffer(req.Body))
	if err != nil {
		return nil, err
	}

	for hdr := range req.Header {
		httpReq.Header.Set(hdr, req.Header.Get(hdr))
	}

	req.SentAt = time.Now()

	pool := c.pool
	if pool == nil {
		pool = azure.DefaultClientPool
	}

	httpRes, err := pool.GetClient(c.timeout).Do(httpReq)
	if err != nil {
		return nil, err
	}
	defer httpRes.Body.Close()

	var buf bytes.Buffer
	if _, err = buf.ReadFrom(httpRes.Body); err != nil {
		return nil, err
	}

	resp := http.Response{
		Body:       buf.Bytes(),
		Request:    req,
		ReceivedAt: time.Now(),
	}

	return &resp, nil
}
예제 #4
0
func (client *Client) Authenticate() error {
	var body []byte
	var cert Certificate
	fmt.Println("endpoint: " + client.Endpoint)

	transport := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	defaultClient := &http.Client{Transport: transport}
	request, err := http.NewRequest("POST", client.Endpoint, nil)

	if err != nil {
		return err
	}
	request.SetBasicAuth(client.UserName, client.Password)
	response, err := defaultClient.Do(request)

	if err != nil {
		return err
	}

	defer response.Body.Close()

	if body, err = getResponse(response); err != nil {
		return err
	}
	fmt.Println("body")
	fmt.Println(body)
	if err = json.Unmarshal(body, &cert); err != nil {
		return err
	}

	client.Cert = cert

	return nil
}