oneops-admin/lib/chef/knife/cloud.rb
def sync( options = {}, comments )
      nspath = "#{Chef::Config[:nspath]}/#{options[:register]}/clouds"
      return false unless ensure_path_exists(nspath)

      Chef::Log.info("Starting sync for cloud #{name} in namespace #{nspath}")
      if (options[:reload])
        puts("Deleting cloud #{self.name} because reload is specified")
        cms_ci_delete( :nsPath => nspath, :ciClassName => 'mgmt.Cloud', :ciName => self.name )
      end

      # check if cloud is ignored, if so, simply return to skip syncing it
      # waiting until after reload so it can be removed before ignored.
      if self.ignore
        puts("Cloud, #{self.name} is set to ignore, skipping...")
        return true
      end

      cloud = Cms::Ci.build( :nsPath => nspath, :ciClassName => 'mgmt.Cloud', :ciName => self.name )
      cloud.ciAttributes.attributes = { "description" => description, "auth" => auth, "is_location" => is_location }
      cloud = cms_ci_sync(cloud)

      # process each service
      puts('services:')
      self.to_hash['services'].each do |service,options|
        ciClassName = options[:cookbook].capitalize
        if options[:source]
          ciClassName = [ 'mgmt.cloud.service', options[:source], ciClassName ].join('.')
        else
          ciClassName = [ 'mgmt.cloud.service', ciClassName ].join('.')
        end
        puts(" - #{service} #{ciClassName}")
        relationName = 'mgmt.Provides'
        relation = Cms::Relation.all( :params => {  :ciId => cloud.id,
          :nsPath => nspath,
          :direction => 'from',
          :relationName => relationName,
          :targetClassName => ciClassName,
          :includeToCi => true
        }).select { |r| r.toCi.ciName == service }.first

        if relation.nil?
          Chef::Log.info( "Creating service #{service}")
          relation = build('Cms::Relation',   :relationName => relationName,
                                              :nsPath => nspath,
                                              :fromCiId => cloud.id
                                 )
          ci = Cms::Ci.first( :params => { :nsPath => "#{nspath}/#{cloud.ciName}", :ciClassName => ciClassName, :ciName => service })

          if ci.nil?
            relation.toCiId = 0
            relation.toCi = build('Cms::Ci',  :nsPath => "#{nspath}/#{cloud.ciName}",
                                              :ciClassName => ciClassName,
                                              :ciName => service
                                           )
          else
            relation.toCiId = ci.id
            relation.toCi = ci
            Log.debug(relation.inspect)
            # if relation is missing, but ci is present, save the relation only first
            if cms_relation_sync(relation)
              Chef::Log.info("Successfuly saved service #{service}")
              relation = Cms::Relation.find(relation.id, :params => {  :nsPath => nspath, :includeToCi => true } )
            else
              Chef::Log.error("Could not save service #{service}, skipping it")
            end
          end
          unless relation
            Chef::Log.error("Could not build service #{service}, skipping it")
            next
          end

        else
          Chef::Log.info("Updating service #{service}")
        end

        relation.comments = comments
        relation.toCi.comments = comments

        # provides relation attributes
        relation.relationAttributes.attributes.each do |name,value|
          if options[:provides][name]
            relation.relationAttributes.send(name+'=',options[:provides][name])
          end
        end

        # service attributes
        relation.toCi.ciAttributes.attributes.each do |name,value|
          if options[:attributes] && options[:attributes].has_key?(name)
            relation.toCi.ciAttributes.send(name+'=',options[:attributes][name])
          end
        end

        Log.debug(relation.inspect)
        if cms_relation_sync(relation)
          Chef::Log.info("Successfuly saved service #{service}")
        else
          Chef::Log.error("Could not save service #{service}, skipping it")
        end

        cms_offering_sync(options[:offerings],relation.toCi)
      end

      Chef::Log.info("Completed sync for cloud #{name}!")
      self
    end
# Sync this service via the REST API
Sync this service via the REST API
Sync this service via the REST API
def sync( options = {}, comments )
      nspath = "#{Chef::Config[:nspath]}/#{options[:register]}/clouds"
      return false unless ensure_path_exists(nspath)
      Chef::Log.info("Starting sync for cloud #{name} in namespace #{nspath}")
      if (options[:reload])
        puts("Deleting cloud #{self.name} because reload is specified")
        cms_ci_delete( :nsPath => nspath, :ciClassName => 'mgmt.Cloud', :ciName => self.name )
      end
      if self.ignore
        puts("Cloud, #{self.name} is set to ignore, skipping...")
        return true
      end
      cloud = Cms::Ci.build( :nsPath => nspath, :ciClassName => 'mgmt.Cloud', :ciName => self.name )
      cloud.ciAttributes.attributes = { "description" => description, "auth" => auth, "is_location" => is_location }
      cloud = cms_ci_sync(cloud)
      puts('services:')
      self.to_hash['services'].each do |service,options|
        ciClassName = options[:cookbook].capitalize
        if options[:source]
          ciClassName = [ 'mgmt.cloud.service', options[:source], ciClassName ].join('.')
        else
          ciClassName = [ 'mgmt.cloud.service', ciClassName ].join('.')
        end
        puts(" - #{service} #{ciClassName}")
        relationName = 'mgmt.Provides'
        relation = Cms::Relation.all( :params => {  :ciId => cloud.id,
          :nsPath => nspath,
          :direction => 'from',
          :relationName => relationName,
          :targetClassName => ciClassName,
          :includeToCi => true
        }).select { |r| r.toCi.ciName == service }.first
        if relation.nil?
          Chef::Log.info( "Creating service #{service}")
          relation = build('Cms::Relation',   :relationName => relationName,
                                              :nsPath => nspath,
                                              :fromCiId => cloud.id
                                 )
          ci = Cms::Ci.first( :params => { :nsPath => "#{nspath}/#{cloud.ciName}", :ciClassName => ciClassName, :ciName => service })
          if ci.nil?
            relation.toCiId = 0
            relation.toCi = build('Cms::Ci',  :nsPath => "#{nspath}/#{cloud.ciName}",
                                              :ciClassName => ciClassName,
                                              :ciName => service
                                           )
          else
            relation.toCiId = ci.id
            relation.toCi = ci
            Log.debug(relation.inspect)
            if cms_relation_sync(relation)
              Chef::Log.info("Successfuly saved service #{service}")
              relation = Cms::Relation.find(relation.id, :params => {  :nsPath => nspath, :includeToCi => true } )
            else
              Chef::Log.error("Could not save service #{service}, skipping it")
            end
          end
          unless relation
            Chef::Log.error("Could not build service #{service}, skipping it")
            next
          end
        else
          Chef::Log.info("Updating service #{service}")
        end
        relation.comments = comments
        relation.toCi.comments = comments
        relation.relationAttributes.attributes.each do |name,value|
          if options[:provides][name]
            relation.relationAttributes.send(name+'=',options[:provides][name])
          end
        end
        relation.toCi.ciAttributes.attributes.each do |name,value|
          if options[:attributes] && options[:attributes].has_key?(name)
            relation.toCi.ciAttributes.send(name+'=',options[:attributes][name])
          end
        end
        Log.debug(relation.inspect)
        if cms_relation_sync(relation)
          Chef::Log.info("Successfuly saved service #{service}")
        else
          Chef::Log.error("Could not save service #{service}, skipping it")
        end
        cms_offering_sync(options[:offerings],relation.toCi)
      end
      Chef::Log.info("Completed sync for cloud #{name}!")
      self
    end
def sync ( options = { } , comments ) nspath = "#{Chef::Config[:nspath]}/#{options[:register]}/clouds" return false unless ensure_path_exists ( nspath ) Chef :: Log . info ( "Starting sync for cloud #{name} in namespace #{nspath}" ) if ( options [ :reload ] ) puts ( "Deleting cloud #{self.name} because reload is specified" ) cms_ci_delete ( :nsPath => nspath , :ciClassName => 'mgmt.Cloud' , :ciName => self . name ) end if self . ignore puts ( "Cloud, #{self.name} is set to ignore, skipping..." ) return true end cloud = Cms :: Ci . build ( :nsPath => nspath , :ciClassName => 'mgmt.Cloud' , :ciName => self . name ) cloud . ciAttributes . attributes = { "description" => description , "auth" => auth , "is_location" => is_location } cloud = cms_ci_sync ( cloud ) puts ( 'services:' ) self . to_hash [ 'services' ] . each do | service , options | ciClassName = options [ :cookbook ] . capitalize if options [ :source ] ciClassName = [ 'mgmt.cloud.service' , options [ :source ] , ciClassName ] . join ( '.' ) else ciClassName = [ 'mgmt.cloud.service' , ciClassName ] . join ( '.' ) end puts ( " - #{service} #{ciClassName}" ) relationName = 'mgmt.Provides' relation = Cms :: Relation . all ( :params => { :ciId => cloud . id , :nsPath => nspath , :direction => 'from' , :relationName => relationName , :targetClassName => ciClassName , :includeToCi => true } ) . select { | r | r . toCi . ciName == service } . first if relation . nil? Chef :: Log . info ( "Creating service #{service}" ) relation = build ( 'Cms::Relation' , :relationName => relationName , :nsPath => nspath , :fromCiId => cloud . id ) ci = Cms :: Ci . first ( :params => { :nsPath => "#{nspath}/#{cloud.ciName}" , :ciClassName => ciClassName , :ciName => service } ) if ci . nil? relation . toCiId = 0 relation . toCi = build ( 'Cms::Ci' , :nsPath => "#{nspath}/#{cloud.ciName}" , :ciClassName => ciClassName , :ciName => service ) else relation . toCiId = ci . id relation . toCi = ci Log . debug ( relation . inspect ) if cms_relation_sync ( relation ) Chef :: Log . info ( "Successfuly saved service #{service}" ) relation = Cms :: Relation . find ( relation . id , :params => { :nsPath => nspath , :includeToCi => true } ) else Chef :: Log . error ( "Could not save service #{service}, skipping it" ) end end unless relation Chef :: Log . error ( "Could not build service #{service}, skipping it" ) next end else Chef :: Log . info ( "Updating service #{service}" ) end relation . comments = comments relation . toCi . comments = comments relation . relationAttributes . attributes . each do | name , value | if options [ :provides ] [ name ] relation . relationAttributes . send ( name + '=' , options [ :provides ] [ name ] ) end end relation . toCi . ciAttributes . attributes . each do | name , value | if options [ :attributes ] && options [ :attributes ] . has_key? ( name ) relation . toCi . ciAttributes . send ( name + '=' , options [ :attributes ] [ name ] ) end end Log . debug ( relation . inspect ) if cms_relation_sync ( relation ) Chef :: Log . info ( "Successfuly saved service #{service}" ) else Chef :: Log . error ( "Could not save service #{service}, skipping it" ) end cms_offering_sync ( options [ :offerings ] , relation . toCi ) end Chef :: Log . info ( "Completed sync for cloud #{name}!" ) self end
Sync this service via the REST API
Sync this service via the REST API
# check if cloud is ignored, if so, simply return to skip syncing it # waiting until after reload so it can be removed before ignored. # process each service # if relation is missing, but ci is present, save the relation only first # provides relation attributes # service attributes
options
comments