Class: VCenterDriver::RESTClient

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_client.rb

Overview

Class RESTClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ RESTClient

Returns a new instance of RESTClient.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rest_client.rb', line 30

def initialize(opts)
    @opts = {
        :insecure => true,
        :associable_types => [
            ClusterComputeResource,
            DistributedVirtualSwitch,
            VmwareDistributedVirtualSwitch,
            LibraryItem,
            ResourcePool,
            Folder,
            HostNetwork,
            DistributedVirtualPortgroup,
            VirtualApp,
            StoragePod,
            Datastore,
            Network,
            Datacenter,
            Library,
            HostSystem,
            OpaqueNetwork,
            VirtualMachine
        ],
        :category_name => 'OpenNebula',
        :category_description => 'OpenNebula Category',
    :cardinality =>
        VSphereAutomation::CIS::CisTaggingCategoryModelCardinality
            .const_get(
                'multiple'.upcase
            )
    }.merge(opts)

    @configuration = VSphereAutomation::Configuration.new.tap do |c|
        c.host = @opts[:hostname]
        c.username = @opts[:username]
        c.password = @opts[:password]
        c.scheme = 'https'
        c.verify_ssl = !@opts[:insecure]
        c.verify_ssl_host = !@opts[:insecure]
    end
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



28
29
30
# File 'lib/rest_client.rb', line 28

def configuration
  @configuration
end

Class Method Details

.new_from_host(host_id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rest_client.rb', line 71

def self.new_from_host(host_id)
    client = OpenNebula::Client.new
    host = OpenNebula::Host.new_with_id(host_id, client)
    rc = host.info(true)
    if OpenNebula.is_error?(rc)
        raise "Could not get host info for ID: \
               #{host_id} - #{rc.message}"
    end

    connection = {
        :hostname => host['TEMPLATE/VCENTER_HOST'],
        :username => host['TEMPLATE/VCENTER_USER'],
        :password => host['TEMPLATE/VCENTER_PASSWORD']
    }

    new(connection)
end

Instance Method Details

#get_or_create_category(api_client, category_name) ⇒ Object



122
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
# File 'lib/rest_client.rb', line 122

def get_or_create_category(api_client, category_name)
    category_api =
        VSphereAutomation::CIS::TaggingCategoryApi.new(api_client)
    category = category_api.list.value.find do |id|
        c = category_api.get(id).value
        break c if c.name == category_name
    end
    if category.nil?
        create_spec =
            VSphereAutomation::CIS::CisTaggingCategoryCreateSpec
            .new(
                name => category_name,
                description => @opts[:category_description],
                associable_types => @opts[:associable_types],
                cardinality => @opts[:cardinality]
            )
        create_model =
            VSphereAutomation::CIS::CisTaggingCategoryCreate
            .new(create_spec => create_spec)

        category = category_api.create(create_model)
        category.value
    else
        category.id
    end
end

#get_or_create_tag(api_client, category_id, tag_name, tag_description) ⇒ Object



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
116
117
118
119
120
# File 'lib/rest_client.rb', line 89

def get_or_create_tag(
    api_client,
    category_id,
    tag_name,
    tag_description
)

    tag_api = VSphereAutomation::CIS::TaggingTagApi.new(api_client)
    tag = tag_api.list.value.find do |id|
        c = tag_api.get(id).value
        break c if c.name == tag_name
    end

    if tag.nil?
        create_spec =
            VSphereAutomation::CIS::CisTaggingTagCreateSpec
            .new(
                name => tag_name,
                description => tag_description,
                category_id => category_id
            )
        create_model = VSphereAutomation::CIS::CisTaggingTagCreate.new(
            create_spec => create_spec
        )

        api_instance =
            VSphereAutomation::CIS::TaggingTagApi.new(api_client)
        api_instance.create(create_model).value
    else
        tag.id
    end
end

#sync_tags(vm) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rest_client.rb', line 149

def sync_tags(vm)
    api_client = VSphereAutomation::ApiClient.new(@configuration)
    VSphereAutomation::CIS::SessionApi.new(api_client).create('')

    association_api =
        VSphereAutomation::CIS::TaggingTagAssociationApi
        .new(api_client)

    vm.vcenter_tags.each do |tag|
        category_name = @opts[:category_name]

        unless tag['CATEGORY_NAME'].nil?
            category_name = tag['CATEGORY_NAME']
        end

        category_id = get_or_create_category(api_client, category_name)

        tag_name = tag['NAME']
        tag_description = tag['DESCRIPTION']

        tag_id =
            get_or_create_tag(
                api_client,
                category_id,
                tag_name,
                tag_description
            )

        request_body =
            VSphereAutomation::CIS::CisTaggingTagAssociationAttach.new

        object_id = VSphereAutomation::CIS::VapiStdDynamicID.new
        object_id.id = vm['_ref']
        object_id.type = 'VirtualMachine'

        request_body.object_id = object_id

        begin
            association_api.attach(tag_id, request_body)
        rescue VSphereAutomation::ApiError => e
            puts "Exception when calling \
                 TaggingTagAssociationApi->attach: #{e}"
        end
    end
end