~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/charmstore/metadata.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package charmstore
 
5
 
 
6
import (
 
7
        "net/http"
 
8
 
 
9
        "github.com/juju/errors"
 
10
        "gopkg.in/juju/charmrepo.v2-unstable"
 
11
)
 
12
 
 
13
// JujuMetadata holds Juju-specific information that will be provided
 
14
// to the charm store.
 
15
type JujuMetadata struct {
 
16
        // ModelUUID identifies the Juju model to the charm store.
 
17
        ModelUUID string
 
18
}
 
19
 
 
20
// IsZero indicates whether or not the metadata is the zero value.
 
21
func (meta JujuMetadata) IsZero() bool {
 
22
        return meta.ModelUUID == ""
 
23
}
 
24
 
 
25
func (meta JujuMetadata) asAttrs() map[string]string {
 
26
        return map[string]string{
 
27
                "environment_uuid": meta.ModelUUID,
 
28
        }
 
29
}
 
30
 
 
31
func (meta JujuMetadata) addToHeader(header http.Header) {
 
32
        attrs := meta.asAttrs()
 
33
        for k, v := range attrs {
 
34
                header.Add(charmrepo.JujuMetadataHTTPHeader, k+"="+v)
 
35
        }
 
36
}
 
37
 
 
38
type httpHeaderSetter interface {
 
39
        // SetHTTPHeader sets custom HTTP headers that will be sent to thei
 
40
        // charm store on each request.
 
41
        SetHTTPHeader(header http.Header)
 
42
}
 
43
 
 
44
// setOnClient sets custom HTTP headers that will be sent to the charm
 
45
// store on each request.
 
46
func (meta JujuMetadata) setOnClient(client BaseClient) error {
 
47
        setter, ok := client.(httpHeaderSetter)
 
48
        if !ok {
 
49
                return errors.NotValidf("charm store client (missing SetHTTPHeader method)")
 
50
        }
 
51
 
 
52
        header := make(http.Header)
 
53
        meta.addToHeader(header)
 
54
        setter.SetHTTPHeader(header)
 
55
        return nil
 
56
}