~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/payload/api/private/helpers.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package private
 
5
 
 
6
// TODO(ericsnow) Eliminate the apiserver/common import if possible.
 
7
// TODO(ericsnow) Eliminate the params import if possible.
 
8
 
 
9
import (
 
10
        "github.com/juju/errors"
 
11
        "gopkg.in/juju/names.v2"
 
12
 
 
13
        "github.com/juju/juju/apiserver/common"
 
14
        "github.com/juju/juju/apiserver/params"
 
15
        "github.com/juju/juju/payload"
 
16
        "github.com/juju/juju/payload/api"
 
17
)
 
18
 
 
19
// NewPayloadResult builds a new PayloadResult from the provided tag
 
20
// and error. NotFound is also set based on the error.
 
21
func NewPayloadResult(id string, err error) PayloadResult {
 
22
        result := payload.Result{
 
23
                ID:       id,
 
24
                Payload:  nil,
 
25
                NotFound: errors.IsNotFound(err),
 
26
                Error:    err,
 
27
        }
 
28
        return Result2api(result)
 
29
}
 
30
 
 
31
// API2Result converts the API result to a payload.Result.
 
32
func API2Result(r PayloadResult) (payload.Result, error) {
 
33
        result := payload.Result{
 
34
                NotFound: r.NotFound,
 
35
        }
 
36
 
 
37
        id, err := API2ID(r.Tag)
 
38
        if err != nil {
 
39
                return result, errors.Trace(err)
 
40
        }
 
41
        result.ID = id
 
42
 
 
43
        if r.Payload != nil {
 
44
                pl, err := api.API2Payload(*r.Payload)
 
45
                if err != nil {
 
46
                        return result, errors.Trace(err)
 
47
                }
 
48
                result.Payload = &pl
 
49
        }
 
50
 
 
51
        if r.Error != nil {
 
52
                result.Error = common.RestoreError(r.Error)
 
53
        }
 
54
 
 
55
        return result, nil
 
56
}
 
57
 
 
58
// Result2api converts the payload.Result into a PayloadResult.
 
59
func Result2api(result payload.Result) PayloadResult {
 
60
        res := PayloadResult{
 
61
                NotFound: result.NotFound,
 
62
        }
 
63
 
 
64
        if result.ID != "" {
 
65
                res.Tag = names.NewPayloadTag(result.ID).String()
 
66
        }
 
67
 
 
68
        if result.Payload != nil {
 
69
                pl := api.Payload2api(*result.Payload)
 
70
                res.Payload = &pl
 
71
        }
 
72
 
 
73
        if result.Error != nil {
 
74
                res.Error = common.ServerError(result.Error)
 
75
        }
 
76
 
 
77
        return res
 
78
}
 
79
 
 
80
// API2ID converts the given tag string into a payload ID.
 
81
func API2ID(tagStr string) (string, error) {
 
82
        if tagStr == "" {
 
83
                return tagStr, nil
 
84
        }
 
85
        tag, err := names.ParsePayloadTag(tagStr)
 
86
        if err != nil {
 
87
                return "", errors.Trace(err)
 
88
        }
 
89
        return tag.Id(), nil
 
90
}
 
91
 
 
92
// Payloads2TrackArgs converts the provided payload info into arguments
 
93
// for the Track API endpoint.
 
94
func Payloads2TrackArgs(payloads []payload.Payload) TrackArgs {
 
95
        var args TrackArgs
 
96
        for _, pl := range payloads {
 
97
                fullPayload := payload.FullPayloadInfo{Payload: pl}
 
98
                arg := api.Payload2api(fullPayload)
 
99
                args.Payloads = append(args.Payloads, arg)
 
100
        }
 
101
        return args
 
102
}
 
103
 
 
104
// IDs2ListArgs converts the provided payload IDs into arguments
 
105
// for the List API endpoint.
 
106
func IDs2ListArgs(ids []string) params.Entities {
 
107
        return ids2args(ids)
 
108
}
 
109
 
 
110
// FullIDs2LookUpArgs converts the provided payload "full" IDs into arguments
 
111
// for the LookUp API endpoint.
 
112
func FullIDs2LookUpArgs(fullIDs []string) LookUpArgs {
 
113
        var args LookUpArgs
 
114
        for _, fullID := range fullIDs {
 
115
                name, rawID := payload.ParseID(fullID)
 
116
                args.Args = append(args.Args, LookUpArg{
 
117
                        Name: name,
 
118
                        ID:   rawID,
 
119
                })
 
120
        }
 
121
        return args
 
122
}
 
123
 
 
124
// IDs2SetStatusArgs converts the provided payload IDs into arguments
 
125
// for the SetStatus API endpoint.
 
126
func IDs2SetStatusArgs(ids []string, status string) SetStatusArgs {
 
127
        var args SetStatusArgs
 
128
        for _, id := range ids {
 
129
                arg := SetStatusArg{
 
130
                        Status: status,
 
131
                }
 
132
                arg.Tag = names.NewPayloadTag(id).String()
 
133
                args.Args = append(args.Args, arg)
 
134
        }
 
135
        return args
 
136
}
 
137
 
 
138
// IDs2UntrackArgs converts the provided payload IDs into arguments
 
139
// for the Untrack API endpoint.
 
140
func IDs2UntrackArgs(ids []string) params.Entities {
 
141
        return ids2args(ids)
 
142
}
 
143
 
 
144
func ids2args(ids []string) params.Entities {
 
145
        var args params.Entities
 
146
        for _, id := range ids {
 
147
                tag := names.NewPayloadTag(id).String()
 
148
                args.Entities = append(args.Entities, params.Entity{
 
149
                        Tag: tag,
 
150
                })
 
151
        }
 
152
        return args
 
153
}