cyclid-client 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ # Copyright 2016 Liqwyd Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Cyclid
16
+ module Client
17
+ # Authentication related methods
18
+ module Auth
19
+ # Retrieve a JWT token from the server.
20
+ # @param username [String] User name of the user to retrieve a token for.
21
+ # @param claims [Hash] additional JWT claims to append to the token.
22
+ # @return [Hash] Decoded server response object.
23
+ # @example Request a simple token
24
+ # token_data = token_get
25
+ # @example Request a token with a CSRF injected into the claims
26
+ # token_data = token_get(csrf: 'abcdef0123456789')
27
+ def token_get(username, claims = {})
28
+ uri = server_uri("/token/#{username}")
29
+ res_data = api_json_post(uri, claims)
30
+ @logger.debug res_data
31
+
32
+ return res_data
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ # Copyright 2016 Liqwyd Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Cyclid
16
+ module Client
17
+ # Health-check related methods
18
+ module Health
19
+ # Ping the API server.
20
+ # @return [Boolean] True if the API server is healthy, false if it is unhealthy.
21
+ def health_ping
22
+ uri = server_uri('/health/status')
23
+
24
+ # We need to do without the API helpers as the health endpoint won't
25
+ # return any data, just an HTTP status
26
+ req = authenticate_request(Net::HTTP::Get.new(uri), uri)
27
+ http = Net::HTTP.new(uri.hostname, uri.port)
28
+ res = http.request(req)
29
+
30
+ return res.code == '200'
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,88 @@
1
+ # Copyright 2016 Liqwyd Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Cyclid
16
+ module Client
17
+ # Job related methods
18
+ module Job
19
+ # Submit a job
20
+ # @param organization [String] Organization name.
21
+ # @param job [String] Raw job data in either JSON or YAML
22
+ # @param type [String] Job data format; either 'json' or 'yaml'
23
+ # @return [Hash] Decoded server response object.
24
+ # @example Submit a job in JSON format
25
+ # job = File.read('job.json')
26
+ # job_submit('example', job, 'json')
27
+ # @example Submit a job in YAML format
28
+ # job = File.read('job.yml')
29
+ # job_submit('example', job, 'yaml')
30
+ def job_submit(organization, job, type)
31
+ uri = server_uri("/organizations/#{organization}/jobs")
32
+ case type
33
+ when 'yaml'
34
+ res_data = api_raw_post(uri, job, 'application/x-yaml')
35
+ when 'json'
36
+ res_data = api_raw_post(uri, job, 'application/json')
37
+ else
38
+ raise "Unknown job format #{type}"
39
+ end
40
+ @logger.debug res_data
41
+
42
+ return res_data
43
+ end
44
+
45
+ # Get details of a job
46
+ # @param organization [String] Organization name.
47
+ # @param jobid [Integer] Job ID to retrieve. The ID must be a valid job for the organization.
48
+ # @return [Hash] Decoded server response object.
49
+ # @see #job_status
50
+ # @see #job_log
51
+ def job_get(organization, jobid)
52
+ uri = server_uri("/organizations/#{organization}/jobs/#{jobid}")
53
+ res_data = api_get(uri)
54
+ @logger.debug res_data
55
+
56
+ return res_data
57
+ end
58
+
59
+ # Get a job status
60
+ # @param organization [String] Organization name.
61
+ # @param jobid [Integer] Job ID to retrieve. The ID must be a valid job for the organization.
62
+ # @return [Hash] Decoded server response object.
63
+ # @see #job_get
64
+ # @see #job_log
65
+ def job_status(organization, jobid)
66
+ uri = server_uri("/organizations/#{organization}/jobs/#{jobid}/status")
67
+ res_data = api_get(uri)
68
+ @logger.debug res_data
69
+
70
+ return res_data
71
+ end
72
+
73
+ # Get a job log
74
+ # @param organization [String] Organization name.
75
+ # @param jobid [Integer] Job ID to retrieve. The ID must be a valid job for the organization.
76
+ # @return [Hash] Decoded server response object.
77
+ # @see #job_get
78
+ # @see #job_status
79
+ def job_log(organization, jobid)
80
+ uri = server_uri("/organizations/#{organization}/jobs/#{jobid}/log")
81
+ res_data = api_get(uri)
82
+ @logger.debug res_data
83
+
84
+ return res_data
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,187 @@
1
+ # Copyright 2016 Liqwyd Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Cyclid
16
+ module Client
17
+ # Organization related methods
18
+ module Organization
19
+ # Retrieve the list of organizations from a server
20
+ # @return [Array] List of organization names.
21
+ def org_list
22
+ uri = server_uri('/organizations')
23
+ res_data = api_get(uri)
24
+ @logger.debug res_data
25
+
26
+ orgs = []
27
+ res_data.each do |item|
28
+ orgs << item['name']
29
+ end
30
+
31
+ return orgs
32
+ end
33
+
34
+ # Get details of a specific organization
35
+ # @param name [String] organization name.
36
+ # @return [Hash] Decoded server response object.
37
+ def org_get(name)
38
+ uri = server_uri("/organizations/#{name}")
39
+ res_data = api_get(uri)
40
+ @logger.debug res_data
41
+
42
+ return res_data
43
+ end
44
+
45
+ # Create a new organization. The organization is created without any
46
+ # members; use org_modify to add a set of users to the organization
47
+ # after it has been created.
48
+ #
49
+ # @param name [String] organization name.
50
+ # @param email [String] organization owners email address.
51
+ # @return [Hash] Decoded server response object.
52
+ # @example Create a new organization 'example'
53
+ # new_org = org_add('example', 'admin@example.com')
54
+ # @see #org_modify
55
+ def org_add(name, email)
56
+ # Create the organization object
57
+ org = { 'name' => name, 'owner_email' => email }
58
+ @logger.debug org
59
+
60
+ # Sign & send the request
61
+ uri = server_uri('/organizations')
62
+ res_data = api_json_post(uri, org)
63
+ @logger.debug res_data
64
+
65
+ return res_data
66
+ end
67
+
68
+ # Modify an organization. Only the owner email address and organization
69
+ # members can be changed; you can not change the name of an organization
70
+ # once it has been created.
71
+ #
72
+ # @note Setting the organization members will *overwrite* the existing
73
+ # set; you should ensure the set of members is complete before you set it.
74
+ # @param name [String] organization name.
75
+ # @param args [Hash] options to modify the organization.
76
+ # @option args [String] owner_email Organization owners email address.
77
+ # @option args [Array] members Set of users who will be organization members.
78
+ # @return [Hash] Decoded server response object.
79
+ # @see #org_add
80
+ # @see #org_delete
81
+ def org_modify(name, args)
82
+ # Create the organization object
83
+ org = {}
84
+
85
+ # Add the owner email address if one was supplied
86
+ org['owner_email'] = args[:owner_email] \
87
+ if args.key? :owner_email and args[:owner_email]
88
+
89
+ # Add the list of members if it was supplied
90
+ org['users'] = args[:members] \
91
+ if args.key? :members and args[:members]
92
+
93
+ @logger.debug org
94
+
95
+ # Sign & send the request
96
+ uri = server_uri("/organizations/#{name}")
97
+ res_data = api_json_put(uri, org)
98
+ @logger.debug res_data
99
+
100
+ return res_data
101
+ end
102
+
103
+ # Get details of an organization member
104
+ # @param name [String] organization name.
105
+ # @param username [String] member username.
106
+ # @return [Hash] Decoded server response object.
107
+ # @see User#user_get
108
+ def org_user_get(name, username)
109
+ uri = server_uri("/organizations/#{name}/members/#{username}")
110
+ res_data = api_get(uri)
111
+ @logger.debug res_data
112
+
113
+ return res_data
114
+ end
115
+
116
+ # Modify the permissions for an organization member
117
+ # @param name [String] organization name.
118
+ # @param username [String] member username.
119
+ # @param permissions [Hash] permissions to apply to the member.
120
+ # @option permissions [Boolean] admin organization 'admin' permission.
121
+ # @option permissions [Boolean] write organization 'write' permission.
122
+ # @option permissions [Boolean] read organization 'read' permission.
123
+ # @return [Hash] Decoded server response object.
124
+ # @see #org_modify
125
+ # @example Give the user 'leslie' read & write permission to the 'example' organization
126
+ # perms = {admin: false, write: true, read: true}
127
+ # org_user_permissions('example', 'leslie', perms)
128
+ def org_user_permissions(name, username, permissions)
129
+ perms = { 'permissions' => permissions }
130
+
131
+ @logger.debug perms
132
+
133
+ uri = server_uri("/organizations/#{name}/members/#{username}")
134
+ res_data = api_json_put(uri, perms)
135
+ @logger.debug res_data
136
+
137
+ return res_data
138
+ end
139
+
140
+ # Get a plugin configuration for an organization.
141
+ # @param name [String] organization name.
142
+ # @param type [String] plugin 'type'
143
+ # @param plugin [String] plugin name.
144
+ # @return [Hash] Decoded server response object.
145
+ # @see #org_config_set
146
+ # @example Get the plugin config & schema for the 'foo' 'api' type plugin, for the 'example'
147
+ # organization
148
+ # org_config_get('example', 'api', 'foo')
149
+ def org_config_get(name, type, plugin)
150
+ uri = server_uri("/organizations/#{name}/configs/#{type}/#{plugin}")
151
+ res_data = api_get(uri)
152
+ @logger.debug res_data
153
+
154
+ return res_data
155
+ end
156
+
157
+ # Update a plugin configuration for an organization.
158
+ # @param name [String] organization name.
159
+ # @param type [String] plugin 'type'
160
+ # @param plugin [String] plugin name.
161
+ # @param config [Hash] plugin configuration data.
162
+ # @return [Hash] Decoded server response object.
163
+ # @see #org_config_get
164
+ def org_config_set(name, type, plugin, config)
165
+ uri = server_uri("/organizations/#{name}/configs/#{type}/#{plugin}")
166
+ res_data = api_json_put(uri, config)
167
+ @logger.debug res_data
168
+
169
+ return res_data
170
+ end
171
+
172
+ # Delete an organization
173
+ # @note The API does not currently support deleting an organization and
174
+ # this method will always fail.
175
+ # @param name [String] organization name.
176
+ # @return [Hash] Decoded server response object.
177
+ # @see #org_add
178
+ def org_delete(name)
179
+ uri = server_uri("/organizations/#{name}")
180
+ res_data = api_delete(uri)
181
+ @logger.debug res_data
182
+
183
+ return res_data
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,79 @@
1
+ # Copyright 2016 Liqwyd Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Cyclid
16
+ module Client
17
+ # Stage related methods
18
+ module Stage
19
+ # Retrieve the list of stages from a server
20
+ # @param organization [String] Organization name.
21
+ # @return [Array] The list of stages. Each entry is a hash with the name
22
+ # & version of the stage.
23
+ def stage_list(organization)
24
+ uri = server_uri("/organizations/#{organization}/stages")
25
+ res_data = api_get(uri)
26
+ @logger.debug res_data
27
+
28
+ stages = []
29
+ res_data.each do |item|
30
+ stages << { name: item['name'], version: item['version'] }
31
+ end
32
+
33
+ return stages
34
+ end
35
+
36
+ # Get details of a stage
37
+ # @param organization [String] Organization name.
38
+ # @param name [String] Name of the stage to retrieve.
39
+ # @return [Hash] Decoded server response object.
40
+ def stage_get(organization, name)
41
+ uri = server_uri("/organizations/#{organization}/stages/#{name}")
42
+ res_data = api_get(uri)
43
+ @logger.debug res_data
44
+
45
+ return res_data
46
+ end
47
+
48
+ # Create a stage
49
+ # @param organization [String] Organization name.
50
+ # @param stage [String] Raw stage definition, in JSON format.
51
+ # @return [Hash] Decoded server response object.
52
+ # @see #stage_modify
53
+ # @example Create a new stage from a file
54
+ # stage = File.read('stage.json')
55
+ # stage_create('example, stage)
56
+ def stage_create(organization, stage)
57
+ uri = server_uri("/organizations/#{organization}/stages")
58
+ res_data = api_json_post(uri, stage)
59
+ @logger.debug res_data
60
+
61
+ return res_data
62
+ end
63
+
64
+ # Modify a stage.
65
+ # @note Stages are immutable; this actually creates a new version of an existing stage.
66
+ # @param organization [String] Organization name.
67
+ # @param stage [String] Raw stage definition, in JSON format.
68
+ # @return [Hash] Decoded server response object.
69
+ # @see #stage_create
70
+ def stage_modify(organization, stage)
71
+ uri = server_uri("/organizations/#{organization}/stages")
72
+ res_data = api_json_post(uri, stage)
73
+ @logger.debug res_data
74
+
75
+ return res_data
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,134 @@
1
+ # Copyright 2016 Liqwyd Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'bcrypt'
16
+
17
+ module Cyclid
18
+ module Client
19
+ # User related methods
20
+ module User
21
+ # Retrieve the list of users from a server
22
+ # @return [Array] List of user names.
23
+ def user_list
24
+ uri = server_uri('/users')
25
+ res_data = api_get(uri)
26
+ @logger.debug res_data
27
+
28
+ users = []
29
+ res_data.each do |item|
30
+ users << item['username']
31
+ end
32
+
33
+ return users
34
+ end
35
+
36
+ # Get details of a specific user
37
+ # @param username [String] User name of the user to retrieve.
38
+ # @return [Hash] Decoded server response object.
39
+ def user_get(username)
40
+ uri = server_uri("/users/#{username}")
41
+ res_data = api_get(uri)
42
+ @logger.debug res_data
43
+
44
+ return res_data
45
+ end
46
+
47
+ # Create a new user
48
+ # @param username [String] User name of the new user.
49
+ # @param name [String] Users real name.
50
+ # @param email [String] Users email address.
51
+ # @param password [String] Unencrypted initial password
52
+ # @param secret [String] Initial HMAC signing secret
53
+ # @return [Hash] Decoded server response object.
54
+ # @see #user_modify
55
+ # @see #user_delete
56
+ def user_add(username, email, name = nil, password = nil, secret = nil)
57
+ # Create the user object
58
+ user = { 'username' => username, 'email' => email }
59
+
60
+ # Add the real name is one was supplied
61
+ user['name'] = name unless name.nil?
62
+
63
+ # Add the HMAC secret if one was supplied
64
+ user['secret'] = secret unless secret.nil?
65
+
66
+ # Encrypt & add the password if one was supplied
67
+ user['password'] = BCrypt::Password.create(password).to_s unless password.nil?
68
+
69
+ @logger.debug user
70
+
71
+ # Sign & send the request
72
+ uri = server_uri('/users')
73
+ res_data = api_json_post(uri, user)
74
+ @logger.debug res_data
75
+
76
+ return res_data
77
+ end
78
+
79
+ # Modify a user
80
+ # @param username [String] User name of the new user.
81
+ # @param args [Hash] options to modify the user.
82
+ # @option args [String] name Users real name.
83
+ # @option args [String] email Users email address.
84
+ # @option args [String] secret Initial HMAC signing secret
85
+ # @option args [String] password Unencrypted initial password
86
+ # @return [Hash] Decoded server response object.
87
+ # @see #user_add
88
+ # @see #user_delete
89
+ # @example Change the email address of the user 'leslie'
90
+ # user_modify('leslie', email: 'leslie@example.com')
91
+ # @example Change the password & secret of the user 'bob'
92
+ # user_modify('bob', secret: 'sekrit', password: 'm1lkb0ne')
93
+ def user_modify(username, args)
94
+ # Create the user object
95
+ user = {}
96
+
97
+ # Add the real name is one was supplied
98
+ user['name'] = args[:name] if args.key? :name and args[:name]
99
+
100
+ # Add the email address if one was supplied
101
+ user['email'] = args[:email] if args.key? :email and args[:email]
102
+
103
+ # Add the HMAC secret if one was supplied
104
+ user['secret'] = args[:secret] if args.key? :secret and args[:secret]
105
+
106
+ # Encrypt & add the password if one was supplied
107
+ user['password'] = BCrypt::Password.create(args[:password]).to_s \
108
+ if args.key? :password and args[:password]
109
+
110
+ @logger.debug user
111
+
112
+ # Sign & send the request
113
+ uri = server_uri("/users/#{username}")
114
+ res_data = api_json_put(uri, user)
115
+ @logger.debug res_data
116
+
117
+ return res_data
118
+ end
119
+
120
+ # Delete a user
121
+ # @param username [String] User name of the user to delete.
122
+ # @return [Hash] Decoded server response object.
123
+ # @see #user_add
124
+ # @see #user_modify
125
+ def user_delete(username)
126
+ uri = server_uri("/users/#{username}")
127
+ res_data = api_delete(uri)
128
+ @logger.debug res_data
129
+
130
+ return res_data
131
+ end
132
+ end
133
+ end
134
+ end