Browse Source

initial commit

* working Rakefile to run lint, style and unit tests
* providers for domain, endpoint, project, role, service and user
* some initial README info
stable/newton
Jan Klare 2 years ago
commit
e86f258f5a

+ 7
- 0
.gitignore View File

@@ -0,0 +1,7 @@
1
+Berksfile.lock
2
+*~
3
+*#
4
+.#*
5
+\#*#
6
+.*.sw[a-z]
7
+*.un~

+ 1
- 0
.rubocop.yml View File

@@ -0,0 +1 @@
1
+inherit_from: .rubocop_todo.yml

+ 20
- 0
.rubocop_todo.yml View File

@@ -0,0 +1,20 @@
1
+# This configuration was generated by
2
+# `rubocop --auto-gen-config`
3
+# on 2016-08-29 21:29:34 +0200 using RuboCop version 0.39.0.
4
+# The point is for the user to remove these configuration records
5
+# one by one as the offenses are removed from the code base.
6
+# Note that changes in the inspected code, or installation of new
7
+# versions of RuboCop, may require this file to be generated again.
8
+
9
+# Offense count: 7
10
+Style/Documentation:
11
+  Exclude:
12
+    - 'spec/**/*'
13
+    - 'test/**/*'
14
+    - 'libraries/openstack_base.rb'
15
+    - 'libraries/openstack_domain.rb'
16
+    - 'libraries/openstack_endpoint.rb'
17
+    - 'libraries/openstack_project.rb'
18
+    - 'libraries/openstack_role.rb'
19
+    - 'libraries/openstack_service.rb'
20
+    - 'libraries/openstack_user.rb'

+ 23
- 0
Berksfile View File

@@ -0,0 +1,23 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+source 'https://supermarket.chef.io'
18
+
19
+metadata
20
+
21
+# cookbook for testing LWRPs:
22
+cookbook 'openstackclient_test',
23
+         path: 'spec/cookbooks/openstackclient_test'

+ 176
- 0
LICENSE View File

@@ -0,0 +1,176 @@
1
+                                 Apache License
2
+                           Version 2.0, January 2004
3
+                        http://www.apache.org/licenses/
4
+
5
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+   1. Definitions.
8
+
9
+      "License" shall mean the terms and conditions for use, reproduction,
10
+      and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+      "Licensor" shall mean the copyright owner or entity authorized by
13
+      the copyright owner that is granting the License.
14
+
15
+      "Legal Entity" shall mean the union of the acting entity and all
16
+      other entities that control, are controlled by, or are under common
17
+      control with that entity. For the purposes of this definition,
18
+      "control" means (i) the power, direct or indirect, to cause the
19
+      direction or management of such entity, whether by contract or
20
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+      outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+      "You" (or "Your") shall mean an individual or Legal Entity
24
+      exercising permissions granted by this License.
25
+
26
+      "Source" form shall mean the preferred form for making modifications,
27
+      including but not limited to software source code, documentation
28
+      source, and configuration files.
29
+
30
+      "Object" form shall mean any form resulting from mechanical
31
+      transformation or translation of a Source form, including but
32
+      not limited to compiled object code, generated documentation,
33
+      and conversions to other media types.
34
+
35
+      "Work" shall mean the work of authorship, whether in Source or
36
+      Object form, made available under the License, as indicated by a
37
+      copyright notice that is included in or attached to the work
38
+      (an example is provided in the Appendix below).
39
+
40
+      "Derivative Works" shall mean any work, whether in Source or Object
41
+      form, that is based on (or derived from) the Work and for which the
42
+      editorial revisions, annotations, elaborations, or other modifications
43
+      represent, as a whole, an original work of authorship. For the purposes
44
+      of this License, Derivative Works shall not include works that remain
45
+      separable from, or merely link (or bind by name) to the interfaces of,
46
+      the Work and Derivative Works thereof.
47
+
48
+      "Contribution" shall mean any work of authorship, including
49
+      the original version of the Work and any modifications or additions
50
+      to that Work or Derivative Works thereof, that is intentionally
51
+      submitted to Licensor for inclusion in the Work by the copyright owner
52
+      or by an individual or Legal Entity authorized to submit on behalf of
53
+      the copyright owner. For the purposes of this definition, "submitted"
54
+      means any form of electronic, verbal, or written communication sent
55
+      to the Licensor or its representatives, including but not limited to
56
+      communication on electronic mailing lists, source code control systems,
57
+      and issue tracking systems that are managed by, or on behalf of, the
58
+      Licensor for the purpose of discussing and improving the Work, but
59
+      excluding communication that is conspicuously marked or otherwise
60
+      designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+      "Contributor" shall mean Licensor and any individual or Legal Entity
63
+      on behalf of whom a Contribution has been received by Licensor and
64
+      subsequently incorporated within the Work.
65
+
66
+   2. Grant of Copyright License. Subject to the terms and conditions of
67
+      this License, each Contributor hereby grants to You a perpetual,
68
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+      copyright license to reproduce, prepare Derivative Works of,
70
+      publicly display, publicly perform, sublicense, and distribute the
71
+      Work and such Derivative Works in Source or Object form.
72
+
73
+   3. Grant of Patent License. Subject to the terms and conditions of
74
+      this License, each Contributor hereby grants to You a perpetual,
75
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+      (except as stated in this section) patent license to make, have made,
77
+      use, offer to sell, sell, import, and otherwise transfer the Work,
78
+      where such license applies only to those patent claims licensable
79
+      by such Contributor that are necessarily infringed by their
80
+      Contribution(s) alone or by combination of their Contribution(s)
81
+      with the Work to which such Contribution(s) was submitted. If You
82
+      institute patent litigation against any entity (including a
83
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+      or a Contribution incorporated within the Work constitutes direct
85
+      or contributory patent infringement, then any patent licenses
86
+      granted to You under this License for that Work shall terminate
87
+      as of the date such litigation is filed.
88
+
89
+   4. Redistribution. You may reproduce and distribute copies of the
90
+      Work or Derivative Works thereof in any medium, with or without
91
+      modifications, and in Source or Object form, provided that You
92
+      meet the following conditions:
93
+
94
+      (a) You must give any other recipients of the Work or
95
+          Derivative Works a copy of this License; and
96
+
97
+      (b) You must cause any modified files to carry prominent notices
98
+          stating that You changed the files; and
99
+
100
+      (c) You must retain, in the Source form of any Derivative Works
101
+          that You distribute, all copyright, patent, trademark, and
102
+          attribution notices from the Source form of the Work,
103
+          excluding those notices that do not pertain to any part of
104
+          the Derivative Works; and
105
+
106
+      (d) If the Work includes a "NOTICE" text file as part of its
107
+          distribution, then any Derivative Works that You distribute must
108
+          include a readable copy of the attribution notices contained
109
+          within such NOTICE file, excluding those notices that do not
110
+          pertain to any part of the Derivative Works, in at least one
111
+          of the following places: within a NOTICE text file distributed
112
+          as part of the Derivative Works; within the Source form or
113
+          documentation, if provided along with the Derivative Works; or,
114
+          within a display generated by the Derivative Works, if and
115
+          wherever such third-party notices normally appear. The contents
116
+          of the NOTICE file are for informational purposes only and
117
+          do not modify the License. You may add Your own attribution
118
+          notices within Derivative Works that You distribute, alongside
119
+          or as an addendum to the NOTICE text from the Work, provided
120
+          that such additional attribution notices cannot be construed
121
+          as modifying the License.
122
+
123
+      You may add Your own copyright statement to Your modifications and
124
+      may provide additional or different license terms and conditions
125
+      for use, reproduction, or distribution of Your modifications, or
126
+      for any such Derivative Works as a whole, provided Your use,
127
+      reproduction, and distribution of the Work otherwise complies with
128
+      the conditions stated in this License.
129
+
130
+   5. Submission of Contributions. Unless You explicitly state otherwise,
131
+      any Contribution intentionally submitted for inclusion in the Work
132
+      by You to the Licensor shall be under the terms and conditions of
133
+      this License, without any additional terms or conditions.
134
+      Notwithstanding the above, nothing herein shall supersede or modify
135
+      the terms of any separate license agreement you may have executed
136
+      with Licensor regarding such Contributions.
137
+
138
+   6. Trademarks. This License does not grant permission to use the trade
139
+      names, trademarks, service marks, or product names of the Licensor,
140
+      except as required for reasonable and customary use in describing the
141
+      origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+   7. Disclaimer of Warranty. Unless required by applicable law or
144
+      agreed to in writing, Licensor provides the Work (and each
145
+      Contributor provides its Contributions) on an "AS IS" BASIS,
146
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+      implied, including, without limitation, any warranties or conditions
148
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+      PARTICULAR PURPOSE. You are solely responsible for determining the
150
+      appropriateness of using or redistributing the Work and assume any
151
+      risks associated with Your exercise of permissions under this License.
152
+
153
+   8. Limitation of Liability. In no event and under no legal theory,
154
+      whether in tort (including negligence), contract, or otherwise,
155
+      unless required by applicable law (such as deliberate and grossly
156
+      negligent acts) or agreed to in writing, shall any Contributor be
157
+      liable to You for damages, including any direct, indirect, special,
158
+      incidental, or consequential damages of any character arising as a
159
+      result of this License or out of the use or inability to use the
160
+      Work (including but not limited to damages for loss of goodwill,
161
+      work stoppage, computer failure or malfunction, or any and all
162
+      other commercial damages or losses), even if such Contributor
163
+      has been advised of the possibility of such damages.
164
+
165
+   9. Accepting Warranty or Additional Liability. While redistributing
166
+      the Work or Derivative Works thereof, You may choose to offer,
167
+      and charge a fee for, acceptance of support, warranty, indemnity,
168
+      or other liability obligations and/or rights consistent with this
169
+      License. However, in accepting such obligations, You may act only
170
+      on Your own behalf and on Your sole responsibility, not on behalf
171
+      of any other Contributor, and only if You agree to indemnify,
172
+      defend, and hold each Contributor harmless for any liability
173
+      incurred by, or claims asserted against, such Contributor by reason
174
+      of your accepting any such warranty or additional liability.
175
+
176
+   END OF TERMS AND CONDITIONS

+ 49
- 0
README.md View File

@@ -0,0 +1,49 @@
1
+# Description
2
+
3
+Installs the fog-openstack gem and offers Resources to use it.
4
+
5
+# Requirements
6
+
7
+- gem 'fog-openstack'
8
+
9
+# Resources
10
+
11
+## openstack_domain
12
+- creates or deletes an openstack domain
13
+- example recipe can be found
14
+  [here](spec/cookbooks/openstackclient_test/recipes/domain.rb)
15
+
16
+## openstack_endpoint
17
+- creates or deletes an openstack endpoint
18
+- example recipe can be found
19
+  [here](spec/cookbooks/openstackclient_test/recipes/endpoint.rb)
20
+
21
+## openstack_project
22
+- creates or deletes an openstack project
23
+- example recipe can be found
24
+  [here](spec/cookbooks/openstackclient_test/recipes/project.rb)
25
+
26
+## openstack_role
27
+- creates or deletes an openstack role
28
+- example recipe can be found
29
+  [here](spec/cookbooks/openstackclient_test/recipes/role.rb)
30
+
31
+## openstack_service
32
+- creates or deletes an openstack service
33
+- example recipe can be found
34
+  [here](spec/cookbooks/openstackclient_test/recipes/service.rb)
35
+
36
+## openstack_user
37
+- creates or deletes an openstack user
38
+- grants or revokes role to user in project
39
+- grants or revokes domain to user
40
+- example recipe can be found
41
+  [here](spec/cookbooks/openstackclient_test/recipes/user.rb)
42
+
43
+# License and Maintainer
44
+
45
+Maintainer:: cloudbau GmbH (<j.klare@cloudbau.de>)
46
+Source:: https://github.com/cloudbau/cookbook-openstackclient
47
+Issues:: https://github.com/cloudbau/cookbook-openstackclient/issues
48
+
49
+License:: Apache v2.0

+ 34
- 0
Rakefile View File

@@ -0,0 +1,34 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+task default: ['test']
18
+
19
+task test: [:lint, :style, :unit]
20
+
21
+desc 'Run FoodCritic (lint) tests'
22
+task :lint do
23
+  sh %(chef exec foodcritic --epic-fail any .)
24
+end
25
+
26
+desc 'Run RuboCop (style) tests'
27
+task :style do
28
+  sh %(chef exec rubocop)
29
+end
30
+
31
+desc 'Run RSpec (unit) tests'
32
+task :unit do
33
+  sh %(chef exec rspec --format documentation)
34
+end

+ 97
- 0
libraries/matchers.rb View File

@@ -0,0 +1,97 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+if defined?(ChefSpec)
18
+  def create_openstack_project(resource_name)
19
+    ChefSpec::Matchers::ResourceMatcher
20
+      .new(:openstack_project, :create, resource_name)
21
+  end
22
+
23
+  def delete_openstack_project(resource_name)
24
+    ChefSpec::Matchers::ResourceMatcher
25
+      .new(:openstack_project, :delete, resource_name)
26
+  end
27
+
28
+  def create_openstack_service(resource_name)
29
+    ChefSpec::Matchers::ResourceMatcher
30
+      .new(:openstack_service, :create, resource_name)
31
+  end
32
+
33
+  def delete_openstack_service(resource_name)
34
+    ChefSpec::Matchers::ResourceMatcher
35
+      .new(:openstack_service, :delete, resource_name)
36
+  end
37
+
38
+  def create_openstack_endpoint(resource_name)
39
+    ChefSpec::Matchers::ResourceMatcher
40
+      .new(:openstack_endpoint, :create, resource_name)
41
+  end
42
+
43
+  def delete_openstack_endpoint(resource_name)
44
+    ChefSpec::Matchers::ResourceMatcher
45
+      .new(:openstack_endpoint, :delete, resource_name)
46
+  end
47
+
48
+  def create_openstack_role(resource_name)
49
+    ChefSpec::Matchers::ResourceMatcher
50
+      .new(:openstack_role, :create, resource_name)
51
+  end
52
+
53
+  def delete_openstack_role(resource_name)
54
+    ChefSpec::Matchers::ResourceMatcher
55
+      .new(:openstack_role, :delete, resource_name)
56
+  end
57
+
58
+  def create_openstack_user(resource_name)
59
+    ChefSpec::Matchers::ResourceMatcher
60
+      .new(:openstack_user, :create, resource_name)
61
+  end
62
+
63
+  def delete_openstack_user(resource_name)
64
+    ChefSpec::Matchers::ResourceMatcher
65
+      .new(:openstack_user, :delete, resource_name)
66
+  end
67
+
68
+  def grant_role_openstack_user(resource_name)
69
+    ChefSpec::Matchers::ResourceMatcher
70
+      .new(:openstack_user, :grant_role, resource_name)
71
+  end
72
+
73
+  def revoke_role_openstack_user(resource_name)
74
+    ChefSpec::Matchers::ResourceMatcher
75
+      .new(:openstack_user, :revoke_role, resource_name)
76
+  end
77
+
78
+  def grant_domain_openstack_user(resource_name)
79
+    ChefSpec::Matchers::ResourceMatcher
80
+      .new(:openstack_user, :grant_domain, resource_name)
81
+  end
82
+
83
+  def revoke_domain_openstack_user(resource_name)
84
+    ChefSpec::Matchers::ResourceMatcher
85
+      .new(:openstack_user, :revoke_domain, resource_name)
86
+  end
87
+
88
+  def create_openstack_domain(resource_name)
89
+    ChefSpec::Matchers::ResourceMatcher
90
+      .new(:openstack_domain, :create, resource_name)
91
+  end
92
+
93
+  def delete_openstack_domain(resource_name)
94
+    ChefSpec::Matchers::ResourceMatcher
95
+      .new(:openstack_domain, :delete, resource_name)
96
+  end
97
+end

+ 25
- 0
libraries/openstack_base.rb View File

@@ -0,0 +1,25 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+module OpenstackclientCookbook
18
+  class OpenstackBase < Chef::Resource
19
+    require 'fog/openstack'
20
+    def connection
21
+      @connection_cache ||= Fog::Identity::OpenStack
22
+                            .new(new_resource.connection_params)
23
+    end
24
+  end
25
+end

+ 48
- 0
libraries/openstack_domain.rb View File

@@ -0,0 +1,48 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require_relative 'openstack_base'
18
+module OpenstackclientCookbook
19
+  class OpenstackDomain < OpenstackBase
20
+    resource_name :openstack_domain
21
+
22
+    property :domain_name, String, name_property: true
23
+    property :connection_params, Hash, required: true
24
+
25
+    default_action :create
26
+
27
+    action :create do
28
+      domain = connection.domains.find { |u| u.id == domain_name } ||
29
+               connection.domains.find { |u| u.name == domain_name }
30
+      if domain
31
+        log "Domain with name: \"#{domain_name}\" already exists"
32
+      else
33
+        connection.domains.create(
34
+          name: domain_name
35
+        )
36
+      end
37
+    end
38
+
39
+    action :delete do
40
+      domain = connection.domains.find { |u| u.name == domain_name }
41
+      if domain
42
+        domain.destroy
43
+      else
44
+        log "Domain with name: \"#{domain_name}\" doesn't exist"
45
+      end
46
+    end
47
+  end
48
+end

+ 62
- 0
libraries/openstack_endpoint.rb View File

@@ -0,0 +1,62 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require_relative 'openstack_base'
18
+module OpenstackclientCookbook
19
+  class OpenstackEndpoint < OpenstackBase
20
+    resource_name :openstack_endpoint
21
+
22
+    property :endpoint_name, String, name_property: true
23
+    property :service_name, String, required: true
24
+    property :interface, String, required: true
25
+    property :url, String, required: true
26
+    property :region, String
27
+    property :connection_params, Hash, required: true
28
+
29
+    default_action :create
30
+
31
+    action :create do
32
+      service = connection.services.find { |s| s.name == service_name }
33
+      endpoint = connection.endpoints.find do |e|
34
+        e.service_id == service.id && e.interface == interface
35
+      end
36
+
37
+      if endpoint
38
+        log "#{interface}_endpoint for \"#{service_name}\" already exists"
39
+      else
40
+        connection.endpoints.create(
41
+          interface: interface,
42
+          url: url,
43
+          service_id: service.id,
44
+          name: endpoint_name,
45
+          region: region
46
+        )
47
+      end
48
+    end
49
+
50
+    action :delete do
51
+      service = connection.services.find { |s| s.name == service_name }
52
+      endpoint = connection.endpoints.find do |e|
53
+        e.service_id == service.id && e.interface == interface
54
+      end
55
+      if endpoint
56
+        connection.endpoints.destroy(endpoint.id)
57
+      else
58
+        log "#{interface}_endpoint for \"#{service_name}\" doesn't exist"
59
+      end
60
+    end
61
+  end
62
+end

+ 45
- 0
libraries/openstack_project.rb View File

@@ -0,0 +1,45 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require_relative 'openstack_base'
18
+module OpenstackclientCookbook
19
+  class OpenstackProject < OpenstackBase
20
+    resource_name :openstack_project
21
+
22
+    property :project_name, String, name_property: true
23
+    property :connection_params, Hash, required: true
24
+
25
+    default_action :create
26
+
27
+    action :create do
28
+      project = connection.projects.find { |p| p.name == project_name }
29
+      if project
30
+        log "Project with name: \"#{project_name}\" already exists"
31
+      else
32
+        connection.projects.create name: project_name
33
+      end
34
+    end
35
+
36
+    action :delete do
37
+      project = connection.projects.find { |p| p.name == project_name }
38
+      if project
39
+        project.destroy
40
+      else
41
+        log "Project with name: \"#{project_name}\" doesn't exist"
42
+      end
43
+    end
44
+  end
45
+end

+ 45
- 0
libraries/openstack_role.rb View File

@@ -0,0 +1,45 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require_relative 'openstack_base'
18
+module OpenstackclientCookbook
19
+  class OpenstackRole < OpenstackBase
20
+    resource_name :openstack_role
21
+
22
+    property :role_name, String, name_property: true
23
+    property :connection_params, Hash, required: true
24
+
25
+    default_action :create
26
+
27
+    action :create do
28
+      role = connection.roles.find { |r| r.name == role_name }
29
+      if role
30
+        log "Role with name: \"#{role_name}\" already exists"
31
+      else
32
+        connection.roles.create name: role_name
33
+      end
34
+    end
35
+
36
+    action :delete do
37
+      role = connection.roles.find { |r| r.name == role_name }
38
+      if role
39
+        role.destroy
40
+      else
41
+        log "Role with name: \"#{role_name}\" doesn't exist"
42
+      end
43
+    end
44
+  end
45
+end

+ 49
- 0
libraries/openstack_service.rb View File

@@ -0,0 +1,49 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require_relative 'openstack_base'
18
+module OpenstackclientCookbook
19
+  class OpenstackService < OpenstackBase
20
+    resource_name :openstack_service
21
+
22
+    property :service_name, String, name_property: true
23
+    property :type, String, required: true
24
+    property :connection_params, Hash, required: true
25
+
26
+    default_action :create
27
+
28
+    action :create do
29
+      service = connection.services.find { |s| s.name == service_name }
30
+      if service
31
+        log "Service with name: \"#{service_name}\" already exists"
32
+      else
33
+        connection.services.create(
34
+          name: service_name,
35
+          type: type
36
+        )
37
+      end
38
+    end
39
+
40
+    action :delete do
41
+      service = connection.services.find { |s| s.name == service_name }
42
+      if service
43
+        service.destroy
44
+      else
45
+        log "Service with name: \"#{service_name}\" doesn't exist"
46
+      end
47
+    end
48
+  end
49
+end

+ 82
- 0
libraries/openstack_user.rb View File

@@ -0,0 +1,82 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require_relative 'openstack_base'
18
+module OpenstackclientCookbook
19
+  class OpenstackUser < OpenstackBase
20
+    resource_name :openstack_user
21
+
22
+    property :user_name, String, name_property: true
23
+    property :email, String, default: 'defaultmail'
24
+    property :password, String, default: 'defaultpass'
25
+    property :role_name, String
26
+    property :project_name, String
27
+    property :domain_name, String
28
+    property :connection_params, Hash, required: true
29
+
30
+    default_action :create
31
+
32
+    action :create do
33
+      user = connection.users.find { |u| u.name == user_name }
34
+      if user
35
+        log "User with name: \"#{user_name}\" already exists"
36
+      else
37
+        connection.users.create(
38
+          name: user_name,
39
+          email: email,
40
+          password: password
41
+        )
42
+      end
43
+    end
44
+
45
+    action :delete do
46
+      user = connection.users.find { |u| u.name == user_name }
47
+      if user
48
+        user.destroy
49
+      else
50
+        log "User with name: \"#{user_name}\" doesn't exist"
51
+      end
52
+    end
53
+
54
+    action :grant_role do
55
+      user = connection.users.find { |u| u.name == user_name }
56
+      project = connection.projects.find { |p| p.name == project_name }
57
+      role = connection.roles.find { |r| r.name == role_name }
58
+      project.grant_role_to_user role.id, user.id if role && project && user
59
+    end
60
+
61
+    action :revoke_role do
62
+      user = connection.users.find { |u| u.name == user_name }
63
+      project = connection.projects.find { |p| p.name == project_name }
64
+      role = connection.roles.find { |r| r.name == role_name }
65
+      project.revoke_role_from_user role.id, user.id if role && project && user
66
+    end
67
+
68
+    action :grant_domain do
69
+      user = connection.users.find { |u| u.name == user_name }
70
+      domain = connection.domains.find { |p| p.name == domain_name }
71
+      role = connection.roles.find { |r| r.name == role_name }
72
+      domain.grant_domain_user_role user.id, role.id if role && domain && user
73
+    end
74
+
75
+    action :revoke_domain do
76
+      user = connection.users.find { |u| u.name == user_name }
77
+      domain = connection.domains.find { |p| p.name == domain_name }
78
+      role = connection.roles.find { |r| r.name == role_name }
79
+      domain.revoke_domain_user_role user.id, role.id if role && domain && user
80
+    end
81
+  end
82
+end

+ 26
- 0
metadata.rb View File

@@ -0,0 +1,26 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+name 'openstackclient'
18
+maintainer 'cloudbau GmbH'
19
+maintainer_email 'j.klare@cloudbau.de'
20
+license 'Apache v2.0'
21
+description 'Installs the fog-openstack gem and offers LWRPs to use it'
22
+issues_url 'https://github.com/cloudbau/cookbook-openstackclient/issues'
23
+source_url 'https://github.com/cloudbau/cookbook-openstackclient'
24
+version '0.1.0'
25
+
26
+gem 'fog-openstack'

+ 20
- 0
spec/cookbooks/openstackclient_test/metadata.rb View File

@@ -0,0 +1,20 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+name 'openstackclient_test'
18
+version '0.0.1'
19
+
20
+depends 'openstackclient'

+ 32
- 0
spec/cookbooks/openstackclient_test/recipes/domain.rb View File

@@ -0,0 +1,32 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+connection_params = {
18
+  openstack_auth_url:     'http://devstack.test:5000/v3/auth/tokens',
19
+  openstack_username:     'admin',
20
+  openstack_api_key:      'password',
21
+  openstack_project_name: 'admin',
22
+  openstack_domain_id:    'default'
23
+}
24
+
25
+openstack_domain 'mydomain' do
26
+  connection_params connection_params
27
+end
28
+
29
+openstack_domain 'mydomain' do
30
+  connection_params connection_params
31
+  action :delete
32
+end

+ 40
- 0
spec/cookbooks/openstackclient_test/recipes/endpoint.rb View File

@@ -0,0 +1,40 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+connection_params = {
18
+  openstack_auth_url:     'http://devstack.test:5000/v3/auth/tokens',
19
+  openstack_username:     'admin',
20
+  openstack_api_key:      'password',
21
+  openstack_project_name: 'admin',
22
+  openstack_domain_id:    'default'
23
+}
24
+
25
+%w(public internal admin).each do |interface|
26
+  openstack_endpoint "myendpoint_#{interface}" do
27
+    service_name 'myservice'
28
+    interface interface
29
+    url "http://localhost:80/#{interface}"
30
+    connection_params connection_params
31
+  end
32
+
33
+  openstack_endpoint "myendpoint_#{interface}" do
34
+    service_name 'myservice'
35
+    interface interface
36
+    url "http://localhost:80/#{interface}"
37
+    connection_params connection_params
38
+    action :delete
39
+  end
40
+end

+ 32
- 0
spec/cookbooks/openstackclient_test/recipes/project.rb View File

@@ -0,0 +1,32 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+connection_params = {
18
+  openstack_auth_url:     'http://devstack.test:5000/v3/auth/tokens',
19
+  openstack_username:     'admin',
20
+  openstack_api_key:      'password',
21
+  openstack_project_name: 'admin',
22
+  openstack_domain_id:    'default'
23
+}
24
+
25
+openstack_project 'myproject' do
26
+  connection_params connection_params
27
+end
28
+
29
+openstack_project 'myproject' do
30
+  connection_params connection_params
31
+  action :delete
32
+end

+ 32
- 0
spec/cookbooks/openstackclient_test/recipes/role.rb View File

@@ -0,0 +1,32 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+connection_params = {
18
+  openstack_auth_url:     'http://devstack.test:5000/v3/auth/tokens',
19
+  openstack_username:     'admin',
20
+  openstack_api_key:      'password',
21
+  openstack_project_name: 'admin',
22
+  openstack_domain_id:    'default'
23
+}
24
+
25
+openstack_role 'myrole' do
26
+  connection_params connection_params
27
+end
28
+
29
+openstack_role 'myrole' do
30
+  connection_params connection_params
31
+  action :delete
32
+end

+ 34
- 0
spec/cookbooks/openstackclient_test/recipes/service.rb View File

@@ -0,0 +1,34 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+connection_params = {
18
+  openstack_auth_url:     'http://devstack.test:5000/v3/auth/tokens',
19
+  openstack_username:     'admin',
20
+  openstack_api_key:      'password',
21
+  openstack_project_name: 'admin',
22
+  openstack_domain_id:    'default'
23
+}
24
+
25
+openstack_service 'myservice' do
26
+  type 'mytype'
27
+  connection_params connection_params
28
+end
29
+
30
+openstack_service 'myservice' do
31
+  type 'mytype'
32
+  connection_params connection_params
33
+  action :delete
34
+end

+ 64
- 0
spec/cookbooks/openstackclient_test/recipes/user.rb View File

@@ -0,0 +1,64 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+connection_params = {
18
+  openstack_auth_url:     'http://devstack.test:5000/v3/auth/tokens',
19
+  openstack_username:     'admin',
20
+  openstack_api_key:      'password',
21
+  openstack_project_name: 'admin',
22
+  openstack_domain_id:    'default'
23
+}
24
+
25
+openstack_user 'myuser' do
26
+  email 'myemail'
27
+  password 'mypassword'
28
+  connection_params connection_params
29
+end
30
+
31
+openstack_user 'myuser' do
32
+  connection_params connection_params
33
+  action :delete
34
+end
35
+
36
+openstack_user 'myuser' do
37
+  role_name 'myrole'
38
+  project_name 'myproject'
39
+  connection_params connection_params
40
+  action :grant_role
41
+end
42
+
43
+openstack_user 'myuser' do
44
+  role_name 'myrole'
45
+  project_name 'myproject'
46
+  connection_params connection_params
47
+  action :revoke_role
48
+end
49
+
50
+openstack_user 'myuser' do
51
+  role_name 'myrole'
52
+  project_name 'myproject'
53
+  domain_name 'mydomain'
54
+  connection_params connection_params
55
+  action :grant_domain
56
+end
57
+
58
+openstack_user 'myuser' do
59
+  role_name 'myrole'
60
+  project_name 'myproject'
61
+  domain_name 'mydomain'
62
+  connection_params connection_params
63
+  action :revoke_domain
64
+end

+ 139
- 0
spec/domain_spec.rb View File

@@ -0,0 +1,139 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'spec_helper'
18
+require_relative '../libraries/openstack_domain'
19
+
20
+describe 'openstackclient_test::domain' do
21
+  let(:chef_run) do
22
+    runner = ChefSpec::SoloRunner.new(step_into: ['openstack_domain'])
23
+    runner.converge(described_recipe)
24
+  end
25
+
26
+  let(:domains_empty) do
27
+    double :domains,
28
+           create: true,
29
+           destroy: true,
30
+           find: nil
31
+  end
32
+
33
+  let(:found_domain) do
34
+    double :find,
35
+           destroy: true
36
+  end
37
+
38
+  let(:domains_populated) do
39
+    double :domains,
40
+           create: true,
41
+           destroy: true,
42
+           find: found_domain
43
+  end
44
+
45
+  context 'no domains defined' do
46
+    let(:connection_dub) do
47
+      double :fog_connection,
48
+             domains: domains_empty
49
+    end
50
+
51
+    before do
52
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackDomain)
53
+        .to receive(:connection).and_return(connection_dub)
54
+    end
55
+
56
+    it do
57
+      expect(connection_dub).to receive(:domains)
58
+      chef_run
59
+    end
60
+
61
+    it do
62
+      expect(chef_run).to create_openstack_domain('mydomain')
63
+    end
64
+
65
+    it do
66
+      expect(chef_run).to delete_openstack_domain('mydomain')
67
+    end
68
+
69
+    it do
70
+      expect(chef_run).to write_log(
71
+        'Domain with name: "mydomain" doesn\'t exist'
72
+      )
73
+    end
74
+
75
+    it do
76
+      expect(chef_run).not_to write_log(
77
+        'Domain with name: "mydomain" already exists'
78
+      )
79
+    end
80
+
81
+    it do
82
+      expect(domains_empty).to receive(:create)
83
+        .with(name: 'mydomain')
84
+      chef_run
85
+    end
86
+
87
+    it do
88
+      expect(found_domain).not_to receive(:destory)
89
+      chef_run
90
+    end
91
+  end
92
+
93
+  context 'domains defined' do
94
+    let(:connection_dub) do
95
+      double :fog_connection,
96
+             domains: domains_populated
97
+    end
98
+
99
+    before do
100
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackDomain)
101
+        .to receive(:connection).and_return(connection_dub)
102
+    end
103
+
104
+    it do
105
+      expect(connection_dub).to receive(:domains)
106
+      chef_run
107
+    end
108
+
109
+    it do
110
+      expect(chef_run).to create_openstack_domain('mydomain')
111
+    end
112
+
113
+    it do
114
+      expect(chef_run).to delete_openstack_domain('mydomain')
115
+    end
116
+
117
+    it do
118
+      expect(chef_run).not_to write_log(
119
+        'Domain with name: "mydomain" doesn\'t exist'
120
+      )
121
+    end
122
+
123
+    it do
124
+      expect(chef_run).to write_log(
125
+        'Domain with name: "mydomain" already exists'
126
+      )
127
+    end
128
+
129
+    it do
130
+      expect(domains_populated).not_to receive(:create)
131
+      chef_run
132
+    end
133
+
134
+    it do
135
+      expect(found_domain).to receive(:destroy)
136
+      chef_run
137
+    end
138
+  end
139
+end

+ 165
- 0
spec/endpoint_spec.rb View File

@@ -0,0 +1,165 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'spec_helper'
18
+require_relative '../libraries/openstack_endpoint'
19
+
20
+describe 'openstackclient_test::endpoint' do
21
+  let(:chef_run) do
22
+    runner = ChefSpec::SoloRunner.new(step_into: ['openstack_endpoint'])
23
+    runner.converge(described_recipe)
24
+  end
25
+
26
+  let(:services_dub) do
27
+    double :services,
28
+           find: double(id: 1)
29
+  end
30
+
31
+  let(:endpoints_empty) do
32
+    double :endpoints,
33
+           create: true,
34
+           destroy: true,
35
+           find: nil
36
+  end
37
+
38
+  let(:endpoints_populated) do
39
+    double :endpoints,
40
+           create: true,
41
+           destroy: true,
42
+           find: double(id: 2)
43
+  end
44
+
45
+  context 'no endpoints defined' do
46
+    let(:connection_dub) do
47
+      double :fog_connection,
48
+             services: services_dub,
49
+             endpoints: endpoints_empty
50
+    end
51
+
52
+    before do
53
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackEndpoint)
54
+        .to receive(:connection).and_return(connection_dub)
55
+    end
56
+
57
+    it do
58
+      expect(connection_dub).to receive(:endpoints)
59
+      chef_run
60
+    end
61
+
62
+    it do
63
+      expect(connection_dub).to receive(:services)
64
+      chef_run
65
+    end
66
+
67
+    %w(public internal admin).each do |interface|
68
+      it do
69
+        expect(chef_run).to write_log(
70
+          "#{interface}_endpoint for \"myservice\" doesn't exist"
71
+        )
72
+      end
73
+
74
+      it do
75
+        expect(chef_run).not_to write_log(
76
+          "#{interface}_endpoint for \"myservice\" already exists"
77
+        )
78
+      end
79
+
80
+      it do
81
+        expect(chef_run).to create_openstack_endpoint("myendpoint_#{interface}")
82
+      end
83
+
84
+      it do
85
+        expect(chef_run).to delete_openstack_endpoint("myendpoint_#{interface}")
86
+      end
87
+
88
+      it do
89
+        expect(endpoints_empty).to receive(:create)
90
+          .with(
91
+            interface: interface,
92
+            url: "http://localhost:80/#{interface}",
93
+            service_id: 1,
94
+            name: "myendpoint_#{interface}",
95
+            region: nil
96
+          )
97
+        chef_run
98
+      end
99
+      it do
100
+        expect(endpoints_empty).not_to receive(:destroy)
101
+        chef_run
102
+      end
103
+    end
104
+  end
105
+
106
+  context 'endpoints defined' do
107
+    let(:connection_dub) do
108
+      double :fog_connection,
109
+             services: services_dub,
110
+             endpoints: endpoints_populated
111
+    end
112
+    before do
113
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackEndpoint)
114
+        .to receive(:connection).and_return(connection_dub)
115
+    end
116
+    it do
117
+      expect(connection_dub).to receive(:endpoints)
118
+      chef_run
119
+    end
120
+
121
+    it do
122
+      expect(connection_dub).to receive(:services)
123
+      chef_run
124
+    end
125
+    %w(public internal admin).each do |interface|
126
+      it do
127
+        expect(chef_run).not_to write_log(
128
+          "#{interface}_endpoint for \"myservice\" doesn't exist"
129
+        )
130
+      end
131
+
132
+      it do
133
+        expect(chef_run).to write_log(
134
+          "#{interface}_endpoint for \"myservice\" already exists"
135
+        )
136
+      end
137
+
138
+      it do
139
+        expect(chef_run).to create_openstack_endpoint("myendpoint_#{interface}")
140
+      end
141
+
142
+      it do
143
+        expect(chef_run).to delete_openstack_endpoint("myendpoint_#{interface}")
144
+      end
145
+
146
+      it do
147
+        expect(endpoints_populated).not_to receive(:create)
148
+          .with(
149
+            interface: interface,
150
+            url: "http://localhost:80/#{interface}",
151
+            service_id: 1,
152
+            name: "myendpoint_#{interface}",
153
+            region: nil
154
+          )
155
+        chef_run
156
+      end
157
+
158
+      it do
159
+        expect(endpoints_populated).to receive(:destroy)
160
+          .with(2)
161
+        chef_run
162
+      end
163
+    end
164
+  end
165
+end

+ 141
- 0
spec/project_spec.rb View File

@@ -0,0 +1,141 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'spec_helper'
18
+require_relative '../libraries/openstack_project'
19
+
20
+describe 'openstackclient_test::project' do
21
+  let(:chef_run) do
22
+    runner = ChefSpec::SoloRunner.new(step_into: ['openstack_project'])
23
+    runner.converge(described_recipe)
24
+  end
25
+
26
+  let(:projects_empty) do
27
+    double :projects,
28
+           create: true,
29
+           destroy: true,
30
+           find: nil
31
+  end
32
+
33
+  let(:found_project) do
34
+    double :find,
35
+           destroy: true
36
+  end
37
+
38
+  let(:projects_populated) do
39
+    double :projects,
40
+           create: true,
41
+           destroy: true,
42
+           find: found_project
43
+  end
44
+
45
+  context 'no projects defined' do
46
+    let(:connection_dub) do
47
+      double :fog_connection,
48
+             projects: projects_empty
49
+    end
50
+
51
+    before do
52
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackProject)
53
+        .to receive(:connection).and_return(connection_dub)
54
+    end
55
+
56
+    it do
57
+      expect(connection_dub).to receive(:projects)
58
+      chef_run
59
+    end
60
+
61
+    it do
62
+      expect(chef_run).to create_openstack_project('myproject')
63
+    end
64
+
65
+    it do
66
+      expect(chef_run).to delete_openstack_project('myproject')
67
+    end
68
+
69
+    it do
70
+      expect(chef_run).to write_log(
71
+        'Project with name: "myproject" doesn\'t exist'
72
+      )
73
+    end
74
+
75
+    it do
76
+      expect(chef_run).not_to write_log(
77
+        'Project with name: "myproject" already exists'
78
+      )
79
+    end
80
+
81
+    it do
82
+      expect(projects_empty).to receive(:create)
83
+        .with(
84
+          name: 'myproject'
85
+        )
86
+      chef_run
87
+    end
88
+
89
+    it do
90
+      expect(found_project).not_to receive(:destroy)
91
+      chef_run
92
+    end
93
+  end
94
+
95
+  context 'projects defined' do
96
+    let(:connection_dub) do
97
+      double :fog_connection,
98
+             projects: projects_populated
99
+    end
100
+
101
+    before do
102
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackProject)
103
+        .to receive(:connection).and_return(connection_dub)
104
+    end
105
+
106
+    it do
107
+      expect(connection_dub).to receive(:projects)
108
+      chef_run
109
+    end
110
+
111
+    it do
112
+      expect(chef_run).to create_openstack_project('myproject')
113
+    end
114
+
115
+    it do
116
+      expect(chef_run).to delete_openstack_project('myproject')
117
+    end
118
+
119
+    it do
120
+      expect(chef_run).not_to write_log(
121
+        'Project with name: "myproject" doesn\'t exist'
122
+      )
123
+    end
124
+
125
+    it do
126
+      expect(chef_run).to write_log(
127
+        'Project with name: "myproject" already exists'
128
+      )
129
+    end
130
+
131
+    it do
132
+      expect(projects_empty).not_to receive(:create)
133
+      chef_run
134
+    end
135
+
136
+    it do
137
+      expect(found_project).to receive(:destroy)
138
+      chef_run
139
+    end
140
+  end
141
+end

+ 141
- 0
spec/role_spec.rb View File

@@ -0,0 +1,141 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'spec_helper'
18
+require_relative '../libraries/openstack_role'
19
+
20
+describe 'openstackclient_test::role' do
21
+  let(:chef_run) do
22
+    runner = ChefSpec::SoloRunner.new(step_into: ['openstack_role'])
23
+    runner.converge(described_recipe)
24
+  end
25
+
26
+  let(:roles_empty) do
27
+    double :role,
28
+           create: true,
29
+           destroy: true,
30
+           find: nil
31
+  end
32
+
33
+  let(:found_role) do
34
+    double :find,
35
+           destroy: true
36
+  end
37
+
38
+  let(:roles_populated) do
39
+    double :role,
40
+           create: true,
41
+           destroy: true,
42
+           find: found_role
43
+  end
44
+
45
+  context 'no roles defined' do
46
+    let(:connection_dub) do
47
+      double :fog_connection,
48
+             roles: roles_empty
49
+    end
50
+
51
+    before do
52
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackRole)
53
+        .to receive(:connection).and_return(connection_dub)
54
+    end
55
+
56
+    it do
57
+      expect(connection_dub).to receive(:roles)
58
+      chef_run
59
+    end
60
+
61
+    it do
62
+      expect(chef_run).to create_openstack_role('myrole')
63
+    end
64
+
65
+    it do
66
+      expect(chef_run).to delete_openstack_role('myrole')
67
+    end
68
+
69
+    it do
70
+      expect(chef_run).to write_log(
71
+        'Role with name: "myrole" doesn\'t exist'
72
+      )
73
+    end
74
+
75
+    it do
76
+      expect(chef_run).not_to write_log(
77
+        'Role with name: "myrole" already exists'
78
+      )
79
+    end
80
+
81
+    it do
82
+      expect(roles_empty).to receive(:create)
83
+        .with(
84
+          name: 'myrole'
85
+        )
86
+      chef_run
87
+    end
88
+
89
+    it do
90
+      expect(found_role).not_to receive(:destroy)
91
+      chef_run
92
+    end
93
+  end
94
+
95
+  context 'roles defined' do
96
+    let(:connection_dub) do
97
+      double :fog_connection,
98
+             roles: roles_populated
99
+    end
100
+
101
+    before do
102
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackRole)
103
+        .to receive(:connection).and_return(connection_dub)
104
+    end
105
+
106
+    it do
107
+      expect(connection_dub).to receive(:roles)
108
+      chef_run
109
+    end
110
+
111
+    it do
112
+      expect(chef_run).to create_openstack_role('myrole')
113
+    end
114
+
115
+    it do
116
+      expect(chef_run).to delete_openstack_role('myrole')
117
+    end
118
+
119
+    it do
120
+      expect(chef_run).not_to write_log(
121
+        'Role with name: "myrole" doesn\'t exist'
122
+      )
123
+    end
124
+
125
+    it do
126
+      expect(chef_run).to write_log(
127
+        'Role with name: "myrole" already exists'
128
+      )
129
+    end
130
+
131
+    it do
132
+      expect(roles_empty).not_to receive(:create)
133
+      chef_run
134
+    end
135
+
136
+    it do
137
+      expect(found_role).to receive(:destroy)
138
+      chef_run
139
+    end
140
+  end
141
+end

+ 142
- 0
spec/service_spec.rb View File

@@ -0,0 +1,142 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'spec_helper'
18
+require_relative '../libraries/openstack_service'
19
+
20
+describe 'openstackclient_test::service' do
21
+  let(:chef_run) do
22
+    runner = ChefSpec::SoloRunner.new(step_into: ['openstack_service'])
23
+    runner.converge(described_recipe)
24
+  end
25
+
26
+  let(:services_empty) do
27
+    double :service,
28
+           create: true,
29
+           destroy: true,
30
+           find: nil
31
+  end
32
+
33
+  let(:found_service) do
34
+    double :find,
35
+           destroy: true
36
+  end
37
+
38
+  let(:services_populated) do
39
+    double :service,
40
+           create: true,
41
+           destroy: true,
42
+           find: found_service
43
+  end
44
+
45
+  context 'no services defined' do
46
+    let(:connection_dub) do
47
+      double :fog_connection,
48
+             services: services_empty
49
+    end
50
+
51
+    before do
52
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackService)
53
+        .to receive(:connection).and_return(connection_dub)
54
+    end
55
+
56
+    it do
57
+      expect(connection_dub).to receive(:services)
58
+      chef_run
59
+    end
60
+
61
+    it do
62
+      expect(chef_run).to create_openstack_service('myservice')
63
+    end
64
+
65
+    it do
66
+      expect(chef_run).to delete_openstack_service('myservice')
67
+    end
68
+
69
+    it do
70
+      expect(chef_run).to write_log(
71
+        'Service with name: "myservice" doesn\'t exist'
72
+      )
73
+    end
74
+
75
+    it do
76
+      expect(chef_run).not_to write_log(
77
+        'Service with name: "myservice" already exists'
78
+      )
79
+    end
80
+
81
+    it do
82
+      expect(services_empty).to receive(:create)
83
+        .with(
84
+          name: 'myservice',
85
+          type: 'mytype'
86
+        )
87
+      chef_run
88
+    end
89
+
90
+    it do
91
+      expect(found_service).not_to receive(:destroy)
92
+      chef_run
93
+    end
94
+  end
95
+
96
+  context 'services defined' do
97
+    let(:connection_dub) do
98
+      double :fog_connection,
99
+             services: services_populated
100
+    end
101
+
102
+    before do
103
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackService)
104
+        .to receive(:connection).and_return(connection_dub)
105
+    end
106
+
107
+    it do
108
+      expect(connection_dub).to receive(:services)
109
+      chef_run
110
+    end
111
+
112
+    it do
113
+      expect(chef_run).to create_openstack_service('myservice')
114
+    end
115
+
116
+    it do
117
+      expect(chef_run).to delete_openstack_service('myservice')
118
+    end
119
+
120
+    it do
121
+      expect(chef_run).not_to write_log(
122
+        'Service with name: "myservice" doesn\'t exist'
123
+      )
124
+    end
125
+
126
+    it do
127
+      expect(chef_run).to write_log(
128
+        'Service with name: "myservice" already exists'
129
+      )
130
+    end
131
+
132
+    it do
133
+      expect(services_empty).not_to receive(:create)
134
+      chef_run
135
+    end
136
+
137
+    it do
138
+      expect(found_service).to receive(:destroy)
139
+      chef_run
140
+    end
141
+  end
142
+end

+ 26
- 0
spec/spec_helper.rb View File

@@ -0,0 +1,26 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'chefspec'
18
+require 'chefspec/berkshelf'
19
+require 'fog/openstack'
20
+
21
+RSpec.configure do |config|
22
+  config.color = true
23
+  config.log_level = :error
24
+end
25
+
26
+at_exit { ChefSpec::Coverage.report! }

+ 252
- 0
spec/user_spec.rb View File

@@ -0,0 +1,252 @@
1
+#  encoding: UTF-8
2
+#
3
+#  Copyright 2016 cloudbau GmbH
4
+#
5
+#  Licensed under the Apache License, Version 2.0 (the "License");
6
+#  you may not use this file except in compliance with the License.
7
+#  You may obtain a copy of the License at
8
+#
9
+#      http://www.apache.org/licenses/LICENSE-2.0
10
+#
11
+#  Unless required by applicable law or agreed to in writing, software
12
+#  distributed under the License is distributed on an "AS IS" BASIS,
13
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+#  See the License for the specific language governing permissions and
15
+#  limitations under the License.
16
+
17
+require 'spec_helper'
18
+require_relative '../libraries/openstack_user'
19
+
20
+describe 'openstackclient_test::user' do
21
+  let(:chef_run) do
22
+    runner = ChefSpec::SoloRunner.new(step_into: ['openstack_user'])
23
+    runner.converge(described_recipe)
24
+  end
25
+
26
+  let(:users_empty) do
27
+    double :user,
28
+           create: true,
29
+           destroy: true,
30
+           find: nil
31
+  end
32
+
33
+  let(:found_user) do
34
+    double :find,
35
+           id: 4,
36
+           destroy: true
37
+  end
38
+
39
+  let(:users_populated) do
40
+    double :user,
41
+           create: true,
42
+           destroy: true,
43
+           find: found_user
44
+  end
45
+
46
+  let(:found_role) do
47
+    double :find,
48
+           id: 3
49
+  end
50
+
51
+  let(:roles_populated) do
52
+    double :role,
53
+           find: found_role
54
+  end
55
+
56
+  let(:found_domain) do
57
+    double :find,
58
+           grant_domain_user_role: true,
59
+           revoke_domain_user_role: true
60
+  end
61
+
62
+  let(:domains_populated) do
63
+    double :domains,
64
+           find: found_domain
65
+  end
66
+
67
+  let(:found_project) do
68
+    double :find,
69
+           grant_role_to_user: true,
70
+           revoke_role_from_user: true
71
+  end
72
+
73
+  let(:projects_populated) do
74
+    double :projects,
75
+           find: found_project
76
+  end
77
+
78
+  context 'no users defined' do
79
+    let(:connection_dub) do
80
+      double :fog_connection,
81
+             users: users_empty,
82
+             domains: domains_populated,
83
+             roles: roles_populated,
84
+             projects: projects_populated
85
+    end
86
+
87
+    before do
88
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackUser)
89
+        .to receive(:connection).and_return(connection_dub)
90
+    end
91
+
92
+    it do
93
+      expect(connection_dub).to receive(:users)
94
+      chef_run
95
+    end
96
+
97
+    it do
98
+      expect(connection_dub).to receive(:domains)
99
+      chef_run
100
+    end
101
+
102
+    it do
103
+      expect(connection_dub).to receive(:roles)
104
+      chef_run
105
+    end
106
+
107
+    it do
108
+      expect(connection_dub).to receive(:projects)
109
+      chef_run
110
+    end
111
+
112
+    it do
113
+      expect(chef_run).to create_openstack_user('myuser')
114
+    end
115
+
116
+    it do
117
+      expect(chef_run).to delete_openstack_user('myuser')
118
+    end
119
+
120
+    it do
121
+      expect(chef_run).to write_log(
122
+        'User with name: "myuser" doesn\'t exist'
123
+      )
124
+    end
125
+
126
+    it do
127
+      expect(chef_run).not_to write_log(
128
+        'User with name: "myuser" already exists'
129
+      )
130
+    end
131
+
132
+    it do
133
+      expect(users_empty).to receive(:create)
134
+        .with(
135
+          name: 'myuser',
136
+          email: 'myemail',
137
+          password: 'mypassword'
138
+        )
139
+      chef_run
140
+    end
141
+
142
+    it do
143
+      expect(found_user).not_to receive(:destroy)
144
+      chef_run
145
+    end
146
+  end
147
+
148
+  context 'users defined' do
149
+    let(:connection_dub) do
150
+      double :fog_connection,
151
+             users: users_populated,
152
+             domains: domains_populated,
153
+             roles: roles_populated,
154
+             projects: projects_populated
155
+    end
156
+
157
+    before do
158
+      allow_any_instance_of(OpenstackclientCookbook::OpenstackUser)
159
+        .to receive(:connection).and_return(connection_dub)
160
+    end
161
+
162
+    it do
163
+      expect(connection_dub).to receive(:users)
164
+      chef_run
165
+    end
166
+
167
+    it do
168
+      expect(connection_dub).to receive(:domains)
169
+      chef_run
170
+    end
171
+
172
+    it do
173
+      expect(connection_dub).to receive(:roles)
174
+      chef_run
175
+    end
176
+
177
+    it do
178
+      expect(connection_dub).to receive(:projects)
179
+      chef_run
180
+    end
181
+
182
+    it do
183
+      expect(chef_run).to create_openstack_user('myuser')
184
+    end
185
+
186
+    it do
187
+      expect(chef_run).to delete_openstack_user('myuser')
188
+    end
189
+
190
+    it do
191
+      expect(chef_run).not_to write_log(
192
+        'User with name: "myuser" doesn\'t exist'
193
+      )
194
+    end
195
+
196
+    it do
197
+      expect(chef_run).to write_log(
198
+        'User with name: "myuser" already exists'
199
+      )
200
+    end
201
+
202
+    it do
203
+      expect(chef_run).to grant_role_openstack_user('myuser')
204
+    end
205
+
206
+    it do
207
+      expect(chef_run).to revoke_role_openstack_user('myuser')
208
+    end
209
+
210
+    it do
211
+      expect(chef_run).to grant_domain_openstack_user('myuser')
212
+    end
213
+
214
+    it do
215
+      expect(chef_run).to revoke_domain_openstack_user('myuser')
216
+    end
217
+
218
+    it do
219
+      expect(users_empty).not_to receive(:create)
220
+      chef_run
221
+    end
222
+
223
+    it do
224
+      expect(found_user).to receive(:destroy)
225
+      chef_run
226
+    end
227
+
228
+    it do
229
+      expect(found_project).to receive(:grant_role_to_user)
230
+        .with(3, 4)
231
+      chef_run
232
+    end
233
+
234
+    it do
235
+      expect(found_project).to receive(:revoke_role_from_user)
236
+        .with(3, 4)
237
+      chef_run
238
+    end
239
+
240
+    it do
241
+      expect(found_domain).to receive(:grant_domain_user_role)
242
+        .with(4, 3)
243
+      chef_run
244
+    end
245
+
246
+    it do
247
+      expect(found_domain).to receive(:revoke_domain_user_role)
248
+        .with(4, 3)
249
+      chef_run
250
+    end
251
+  end
252
+end

Loading…
Cancel
Save