Sha256: a3f9dae251ca5e9650034ec2578d78aa032ae4072cad9ca78cbd322f94eb54e2

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

#
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef-analytics/identity'
require 'chef/config'

module ChefAnalytics
  class Knife < Chef::Knife
    attr_accessor :analytics_server_url

    def initialize(arg)
      super(arg)
    end

    #
    # helper functions
    #

    def fetch_token
      identity = ChefAnalytics::Identity.new(identity_server_url: @identity_server_url)
      token = identity.token
      if token.nil?
        ui.error "Couldn't get OAuth2 token from OC-ID server"
        exit 1
      end
      token
    end

    def validate_and_set_params
      @analytics_server_url ||= Chef::Config[:analytics_server_url]
      @analytics_server_url ||= config[:analytics_server_url]
      @identity_server_url ||= Chef::Config[:identity_server_url]
      @identity_server_url ||= config[:identity_server_url]

      unless @analytics_server_url
        ui.error "analytics_server_url not set in config or command line"
        exit 1
      end
    end

    def analytics_server_root
      URI.join(@analytics_server_url, '/').to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chef-analytics-0.1.0 lib/chef-analytics/knife.rb