class DTK::Client::Operation::Service::SetAttribute

Public Class Methods

execute(args = Args.new) click to toggle source
# File lib/client/operation/service/set_attribute.rb, line 21
def self.execute(args = Args.new)
  wrap_operation(args) do |args|
    service_instance = args.required(:service_instance)
    attribute_name   = args[:attribute_name]
    attribute_value  = args[:attribute_value]
    encrypt = args[:encrypt]
    if encrypt
      hash = {
        :name                => attribute_name.split('/').last,
        :component           => attribute_name.split('/').first
      }
      attribute_to_set = rest_get "#{BaseRoute}/#{service_instance}/get_attribute", QueryStringHash.new(hash)

      if attribute_to_set.data.empty? 
        raise Error::Usage, "There is no attribute named #{attribute_name}."
      end

      hash = {
        :name                => "encryption_public_key",
        :component           => attribute_to_set.data["nested_component"]["display_name"]
      }
      public_key_attribute = rest_get "#{BaseRoute}/#{service_instance}/get_attribute", QueryStringHash.new(hash)

      if public_key_attribute.data.empty? || !(public_key_attribute.data["value_asserted"] || public_key_attribute.data["value_derived"])
        raise Error::Usage, "There is no encryption_public_key attribute in component '#{attribute_to_set.data["nested_component"]["display_name"]}', or its' value is not set."
      end

      public_key = public_key_attribute.data["value_asserted"] || public_key_attribute.data["value_derived"]
      attribute_value = DTK::Client::SecurityUtil.encrypt(public_key, attribute_value) 
    end
    query_string_hash = QueryStringHash.new(
      :pattern?  => attribute_name,
      :value?    => attribute_value
    )
    response = rest_post("#{BaseRoute}/#{service_instance}/set_attribute", query_string_hash)

    if repo_updated = response.data["repo_updated"] 
      repo_info_args = Args.new(
       :service_instance => service_instance,
       :branch           => response.required(:branch, :name),
       :repo_url         => response.required(:repo, :url),
       :service_instance_dir => args[:service_instance_dir]
     )
 
     ClientModuleDir::GitRepo.pull_from_service_repo(repo_info_args)
   end
   nil
  end
end