scripts.go 4.39 KB
Newer Older
1
2
3
4
5
6
7
8
package scripts

import (
	"encoding/json"
	"log"
)

type (
Lukas Burgey's avatar
Lukas Burgey committed
9
10
	// Credential is currently a ssh key, but may be a password hash in the future
	Credential struct {
Lukas Burgey's avatar
Lukas Burgey committed
11
		ID    int    `json:"id,omitempty"`
Lukas Burgey's avatar
Lukas Burgey committed
12
13
14
15
16
		Type  string `json:"type,omitempty"`
		Name  string `json:"name"`
		Value string `json:"value"`
	}

17
18
19
20
	// UserCredentialStates serves to inform the backend about the per credential states
	// after the script run
	UserCredentialStates map[string]map[string]State

Lukas Burgey's avatar
Lukas Burgey committed
21
22
23
	// UserCredentials maps a credential type to the credentials of this type
	UserCredentials map[string][]Credential

24
25
26
27
28
29
30
31
32
33
	// Group a group of which User is a member of
	Group struct {
		Name string `json:"name"`
	}

	// UserInfo info about the user
	UserInfo map[string]interface{}

	// User describes the user info
	User struct {
Lukas Burgey's avatar
Lukas Burgey committed
34
35
36
37
38
		// Email string `json:"email"`
		// Groups   []Group  `json:"groups"`

		UserInfo    UserInfo        `json:"userinfo"`
		Credentials UserCredentials `json:"credentials,omitempty"`
39
40
41
42
43
44
45
46
47
48
49
	}

	// Input of the deployment script
	Input struct {
		// StateTarget is the state which is to be reached by this deployment task
		// StateTarget is either Deployed or NotDeployed
		StateTarget State `json:"state_target"`

		// User describes the user of this deployment task
		User User `json:"user"`

Lukas Burgey's avatar
Lukas Burgey committed
50
51
		// Credentials the credentials to deploy
		// maps the credential type to the according credentials
Lukas Burgey's avatar
Lukas Burgey committed
52
53
		// TODO this should move into Input.User
		Credentials UserCredentials `json:"credentials,omitempty"`
Lukas Burgey's avatar
Lukas Burgey committed
54

55
56
57
58
		// Questionnaire is an answered questionnaire
		// Maps a question name to the answer of the user
		// The keys (question names) *must* be identical to those of the Output.Questionnaire
		// containing the questions
Lukas Burgey's avatar
Lukas Burgey committed
59
		Questionnaire map[string]string `json:"questionnaire,omitempty"`
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	}

	// Output of the deployment script
	Output struct {
		// State of the deployment, after the script execution
		// when State == Questionnaire then Output.Questionnaire *must* be set
		// when State == Deployed then Output.Credentials *can* be set
		State State `json:"state"`

		// TODO use this correctly
		// Message for the user
		Msg string `json:"message"`

		// Questionnaire requested by the script
		// Ignored when State is not Questionnaire
		// Maps a question name to a description of the question
Lukas Burgey's avatar
Lukas Burgey committed
76
		Questionnaire map[string]string `json:"questionnaire,omitempty"`
77
78
79
80

		// Credentials for the user
		// Ignored when State is not Deployed
		// Maps a credential name to a credential value
Lukas Burgey's avatar
Lukas Burgey committed
81
		Credentials map[string]string `json:"credentials,omitempty"`
82
83

		// UserCredentialStates states of the credentials provided by the user (not by the script!)
Lukas Burgey's avatar
Lukas Burgey committed
84
85
		// This field is not mandatory! The client will assume that all credentials have the State
		// Output.State if this is not given
Lukas Burgey's avatar
Lukas Burgey committed
86
		UserCredentialStates UserCredentialStates `json:"user_credential_states,omitempty"`
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	}

	// State is a string enum
	// The enum values for State are listed below
	State string
)

const (
	// Deployed value for State
	Deployed State = "deployed"

	// NotDeployed value for State
	NotDeployed State = "not_deployed"

	// Rejected value for State
	// the user can never be deployed
	Rejected State = "rejected"

	// Failed value for State
	// the deployment can be retried
	Failed State = "failed"

	// Questionnaire value for State
	// the user has to fill the questionnaire
	// with the questionnaire data the deployment can be retried
	Questionnaire State = "questionnaire"
)

func (u User) String() string {
Lukas Burgey's avatar
Lukas Burgey committed
116
117
118
119
120
121
122
	if email, ok := u.UserInfo["email"]; ok {
		return email.(string)
	}
	if name, ok := u.UserInfo["name"]; ok {
		return name.(string)
	}
	return ""
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
}

func (i Input) String() string {
	iBytes, err := i.Marshal()
	if err != nil {
		log.Fatal(err)
	}
	return string(iBytes)
}

func (o Output) String() string {
	oBytes, err := o.Marshal()
	if err != nil {
		log.Fatal(err)
	}
	return string(oBytes)
}

// Marshal encodes an Input as json
func (i Input) Marshal() (iBytes []byte, err error) {
	iBytes, err = json.MarshalIndent(i, "", "    ")
	return
}

// Marshal encodes an Output as json
func (o Output) Marshal() (oBytes []byte, err error) {
	oBytes, err = json.MarshalIndent(o, "", "    ")
	return
}

// UnmarshalInput decodes a json encoded input
func UnmarshalInput(inputBytes []byte) (i Input, err error) {
	err = json.Unmarshal(inputBytes, &i)
	return
}

// UnmarshalOutput decodes a json encoded output
func UnmarshalOutput(inputBytes []byte) (i Output, err error) {
	err = json.Unmarshal(inputBytes, &i)
	return
}