Class: TaggingWorkflow

Inherits:
SampleBase show all
Defined in:
/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb

Overview

A tagging workflow sample that demonstrates the tagging CRUD operations.

Constant Summary

TAGGING_MODULE =

poor man’s import facility (it provides a simple name)

Com::Vmware::Cis::Tagging
CATEGORY_CLASS =
TAGGING_MODULE::Category
CATEGORY_MODEL_CLASS =
TAGGING_MODULE::CategoryModel
TAG_CLASS =
TAGGING_MODULE::Tag
TAG_ASSOCIATION_CLASS =
TAGGING_MODULE::TagAssociation
TITLE =
'vAPI Tagging Workflow'
DESCRIPTION =
<<EOL
Demonstrates tagging CRUD operations.
    Step 1: Create a Tag category.
    Step 2: Create a Tag under the category.

    Optional steps to associate a tag with the entity:
    Step 3: Assign the tag to an entity.

    Additional steps when clearData flag is set:
    Step 4: Detach the tag from the entity.
    Step 5: Delete the tag.
    Step 6: Delete the tag category.

    Note: the sample needs an existing entity (supplied by the
    --entity_id and --entit-type options.)
EOL

Constants inherited from SampleBase

SampleBase::LOG_LEVELS

Instance Attribute Summary (collapse)

Attributes inherited from SampleBase

#description, #log, #ls_ip, #ls_url, #mgmt_node_id, #mgmt_node_index, #mgmt_node_name, #multiple_mgmt_node, #option_parser, #options, #sso_password, #sso_username, #title, #use_supporters

Instance Method Summary (collapse)

Methods inherited from SampleBase

#main

Constructor Details

- (TaggingWorkflow) initialize

Constructs a new instance.



52
53
54
55
56
57
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 52

def initialize
    super(TITLE, DESCRIPTION, true)

    @tag_attached = false
    @foo_name = nil
end

Instance Attribute Details

- (Object) category_desc (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def category_desc
  @category_desc
end

- (Object) category_id (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def category_id
  @category_id
end

- (Object) category_name (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def category_name
  @category_name
end

- (Object) category_svc (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def category_svc
  @category_svc
end

- (Object) dynamic_id (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def dynamic_id
  @dynamic_id
end

- (Object) entity_id (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def entity_id
  @entity_id
end

- (Object) entity_type (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def entity_type
  @entity_type
end

- (Object) service_manager (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def service_manager
  @service_manager
end

- (Object) tag_association_svc (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def tag_association_svc
  @tag_association_svc
end

- (Object) tag_attached (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def tag_attached
  @tag_attached
end

- (Object) tag_desc (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def tag_desc
  @tag_desc
end

- (Object) tag_id (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def tag_id
  @tag_id
end

- (Object) tag_name (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def tag_name
  @tag_name
end

- (Object) tag_svc (readonly)

sample attributes



45
46
47
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 45

def tag_svc
  @tag_svc
end

Instance Method Details

- (Object) check_options



85
86
87
88
89
90
91
92
93
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 85

def check_options
    @category_name = check_required_opt(:category_name, '--category-name')
    @tag_name = check_required_opt(:tag_name, '--tag-name')

    @category_desc = options[:category_desc] || category_name
    @tag_desc = options[:tag_desc] || tag_name
    @entity_id = options[:entity_id]
    @entity_type = options[:entity_type] || 'Any'
end

- (Object) cleanup



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 158

def cleanup
    begin
        if tag_attached
            tag_association_svc.detach(tag_id, dynamic_id)
            log.info "Removed tag from entity: #{entity_id}"
        end

        unless tag_id.nil?
            delete_tag(tag_id)
            log.info "Tag deleted; Id: #{tag_id}"
        end

        unless category_id.nil?
            delete_tag_category(category_id)
            log.info "Tag category deleted; Id: #{category_id}"
        end
    rescue Exception => e
        report_error "cleanup failed" + e
    end
end

- (Object) create_tag(name, description, category_id)

Creates a Tag



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 205

def create_tag(name, description, category_id)
    if options[:use_struct_setters]
        create_spec = TAG_CLASS::CreateSpec.new
        create_spec.name = name
        create_spec.description = description
        create_spec.category_id = category_id
    else
        # alternate usage: structure population via constructor hash
        create_spec = TAG_CLASS::CreateSpec.new(
            :name => name,
            :description => description,
            :category_id => category_id)
    end
    return tag_svc.create(create_spec)
end

- (Object) create_tag_category(name, description, cardinality)

create a category. User who invokes this needs create category privilege.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 180

def create_tag_category(name, description, cardinality)
    if options[:use_struct_setters]
        create_spec = CATEGORY_CLASS::CreateSpec.new
        create_spec.name = name
        create_spec.description = description
        create_spec.cardinality = cardinality
        create_spec.associable_types = Set.new
    else
        # alternate usage: structure population via constructor hash
        create_spec = CATEGORY_CLASS::CreateSpec.new(
            :name => name,
            :description => description,
            :cardinality => cardinality,
            :associable_types => Set.new)
    end
    return category_svc.create(create_spec)
end

- (Object) delete_tag(tag_id)

Delete an existing tag. User who invokes this API needs delete privilege on the tag.



236
237
238
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 236

def delete_tag(tag_id)
    tag_svc.delete(tag_id)
end

- (Object) delete_tag_category(category_id)

Deletes an existing tag category. User who invokes this API needs delete privilege on the tag category.



200
201
202
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 200

def delete_tag_category(category_id)
    category_svc.delete(category_id)
end

- (Object) execute



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
148
149
150
151
152
153
154
155
156
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 102

def execute
    log.info 'List all the existing categories...'
    categories = category_svc.list()
    if categories.size > 0
        categories.each { |category|
            log.info "Found Category: #{category}"
        }
    else
        log.info 'No Tag Category Found...'
    end

    log.info 'List all the existing tags...'
    tags = tag_svc.list()
    if tags.size > 0
        tags.each { |tag|
            log.info "Found Tag: #{tag}"
        }
    else
        log.info 'No Tag Found...'
    end

    log.info 'creating a new tag category...'
    @category_id = create_tag_category(category_name, category_desc,
                                       CATEGORY_MODEL_CLASS::Cardinality::MULTIPLE)
    assert(!category_id.nil?, 'create category failed')
    log.info "Tag category created; Id: #{category_id}"

    log.info 'creating a new tag...'
    @tag_id = create_tag(tag_name, tag_desc, category_id)
    assert(!tag_id.nil?, 'create tag failed')
    log.info "Tag created; Id: #{tag_id}"

    log.info 'updating the tag...'
    update_tag(tag_id, 'Server Tag updated at ' + Time.now.utc.to_s)
    log.info "Tag updated; Id: #{tag_id}"

    if entity_id
        # wired conversion to DynamicID
        @dynamic_id = Com::Vmware::Vapi::Std::DynamicID.new
        dynamic_id.type = entity_type
        dynamic_id.id = entity_id
        log.info "Entity ID: #{entity_id}"

        log.info 'Tagging the entity...'
        tag_association_svc.attach(tag_id, dynamic_id)
        for id in tag_association_svc.list_attached_tags(dynamic_id)
            if id == tag_id
                @tag_attached = true
                break
            end
        end
        assert(tag_attached, 'tag attach failed')
        log.info "Tagged entity: #{entity_id}"
    end
end

- (Object) inject_options



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 59

def inject_options
    option_parser.on("-c", "--category-name NAME",
        "Name of the Category to be created") do |value|
        options[:category_name] =  value
    end
    option_parser.on("-C", "--category-desc [TEXT]",
        "Description of the Category to be created") do |value|
        options[:category_desc] =  value
    end
    option_parser.on("-t", "--tag-name NAME",
        "Name of the tag to be created") do |value|
        options[:tag_name] =  value
    end
    option_parser.on("-T", "--tag-desc [TEXT]",
        "Description of the tag to be created") do |value|
        options[:tag_desc] =  value
    end

    option_parser.on("-e", "--entity-id [UUID]", "ID of the entity") do |value|
        options[:entity_id] =  value
    end
    option_parser.on("-f", "--entity-type [TYPE]", "Type of the entity") do |value|
        options[:entity_type] =  value
    end
end

- (Object) setup



95
96
97
98
99
100
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 95

def setup
    @service_manager = get_service_manager()
    @category_svc = CATEGORY_CLASS.new(service_manager.vapi_config)
    @tag_svc = TAG_CLASS.new(service_manager.vapi_config)
    @tag_association_svc = TAG_ASSOCIATION_CLASS.new(service_manager.vapi_config)
end

- (Object) update_tag(tag_id, description)

Update the description of an existing tag. User who invokes this API needs edit privilege on the tag.



223
224
225
226
227
228
229
230
231
232
# File '/build/mts/release/bora-4571906/vcsuite-sdks/ruby/samples/lib/sample/workflow/tagging_workflow.rb', line 223

def update_tag(tag_id, description)
    if options[:use_struct_setters]
        update_spec = TAG_CLASS::UpdateSpec.new
        update_spec.description = description
    else
        update_spec = TAG_CLASS::UpdateSpec.new(
            :description => description)
    end
    tag_svc.update(tag_id, update_spec)
end