github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/artifactory/client.go (about)

     1  package artifactory
     2  
     3  import (
     4  	"crypto/md5"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hugorut/terraform/src/states/remote"
     9  	artifactory "github.com/lusis/go-artifactory/src/artifactory.v401"
    10  )
    11  
    12  const ARTIF_TFSTATE_NAME = "terraform.tfstate"
    13  
    14  type ArtifactoryClient struct {
    15  	nativeClient *artifactory.ArtifactoryClient
    16  	userName     string
    17  	password     string
    18  	url          string
    19  	repo         string
    20  	subpath      string
    21  }
    22  
    23  func (c *ArtifactoryClient) Get() (*remote.Payload, error) {
    24  	p := fmt.Sprintf("%s/%s/%s", c.repo, c.subpath, ARTIF_TFSTATE_NAME)
    25  	output, err := c.nativeClient.Get(p, make(map[string]string))
    26  	if err != nil {
    27  		if strings.Contains(err.Error(), "404") {
    28  			return nil, nil
    29  		}
    30  		return nil, err
    31  	}
    32  
    33  	// TODO: migrate to using X-Checksum-Md5 header from artifactory
    34  	// needs to be exposed by go-artifactory first
    35  
    36  	hash := md5.Sum(output)
    37  	payload := &remote.Payload{
    38  		Data: output,
    39  		MD5:  hash[:md5.Size],
    40  	}
    41  
    42  	// If there was no data, then return nil
    43  	if len(payload.Data) == 0 {
    44  		return nil, nil
    45  	}
    46  
    47  	return payload, nil
    48  }
    49  
    50  func (c *ArtifactoryClient) Put(data []byte) error {
    51  	p := fmt.Sprintf("%s/%s/%s", c.repo, c.subpath, ARTIF_TFSTATE_NAME)
    52  	if _, err := c.nativeClient.Put(p, string(data), make(map[string]string)); err == nil {
    53  		return nil
    54  	} else {
    55  		return fmt.Errorf("Failed to upload state: %v", err)
    56  	}
    57  }
    58  
    59  func (c *ArtifactoryClient) Delete() error {
    60  	p := fmt.Sprintf("%s/%s/%s", c.repo, c.subpath, ARTIF_TFSTATE_NAME)
    61  	err := c.nativeClient.Delete(p)
    62  	return err
    63  }