#
# Copyright:: Copyright (c) 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/knife/cloud/server/create_options"

class Chef
  class Knife
    class Cloud
      module OpenstackServerCreateOptions
        def self.included(includer)
          includer.class_eval do
            include ServerCreateOptions

            # Openstack Server create params.
            option :private_network,
              long: "--openstack-private-network",
              description: "Use the private IP for bootstrapping rather than the public IP",
              boolean: true,
              default: false

            option :openstack_floating_ip,
              short: "-a [IP]",
              long: "--openstack-floating-ip [IP]",
              default: "-1",
              description: "Request to associate a floating IP address to the new OpenStack node. Assumes IPs have been allocated to the project. Specific IP is optional."

            option :openstack_volumes,
              long: "--openstack-volumes VOLUME1,VOLUME2,VOLUME3",
              description: "Comma separated list of the UUID(s) of the volume(s) to attach to the server",
              proc: proc { |volumes| volumes.split(",") }

            option :openstack_scheduler_hints,
              long: "--scheduler-hints HINTS",
              description: "A scheduler group hint to OpenStack"

            option :openstack_security_groups,
              short: "-G X,Y,Z",
              long: "--openstack-groups X,Y,Z",
              description: "The security groups for this server",
              default: ["default"],
              proc: proc { |groups| groups.split(",") }

            option :openstack_ssh_key_id,
              short: "-S KEY",
              long: "--openstack-ssh-key-id KEY",
              description: "The OpenStack SSH keypair id"

            option :user_data,
              long: "--user-data USER_DATA",
              description: "The file path containing user data information for this server",
              proc: proc { |user_data| open(user_data, &:read) }

            option :bootstrap_network,
              long: "--bootstrap-network NAME",
              default: "public",
              description: "Specify network for bootstrapping. Default is 'public'."

            option :network,
              long: "--no-network",
              boolean: true,
              default: true,
              description: "Use first available network for bootstrapping if 'public' and 'private' are unavailable."

            option :network_ids,
              long: "--network-ids NETWORK_ID_1,NETWORK_ID_2,NETWORK_ID_3",
              description: "Comma separated list of the UUID(s) of the network(s) for the server to attach",
              proc: proc { |networks| networks.split(",") }

            option :availability_zone,
              short: "-Z ZONE_NAME",
              long: "--availability-zone ZONE_NAME",
              description: "The availability zone for this server"

            option :metadata,
              short: "-M X=1",
              long: "--metadata X=1",
              description: "Metadata information for this server (may pass multiple times)",
              proc: proc { |data, accumulator|
                accumulator ||= {}
                accumulator.merge!(data.split("=")[0] => data.split("=")[1])
              }

            option :secret_file,
              long: "--secret-file SECRET_FILE",
              description: "A file containing the secret key to use to encrypt data bag item values"

            option :secret,
              long: "--secret ",
              description: "The secret key to use to encrypt data bag item values"
          end
        end
      end
    end
  end
end
