components/puppet/files/solaris/lib/puppet/provider/address_object/solaris.rb
changeset 1409 9db4ba32e740
child 2038 b64efc6f1fe1
equal deleted inserted replaced
1408:8bc5df437e67 1409:9db4ba32e740
       
     1 #
       
     2 # CDDL HEADER START
       
     3 #
       
     4 # The contents of this file are subject to the terms of the
       
     5 # Common Development and Distribution License (the "License").
       
     6 # You may not use this file except in compliance with the License.
       
     7 #
       
     8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9 # or http://www.opensolaris.org/os/licensing.
       
    10 # See the License for the specific language governing permissions
       
    11 # and limitations under the License.
       
    12 #
       
    13 # When distributing Covered Code, include this CDDL HEADER in each
       
    14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15 # If applicable, add the following below this CDDL HEADER, with the
       
    16 # fields enclosed by brackets "[]" replaced with your own identifying
       
    17 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    18 #
       
    19 # CDDL HEADER END
       
    20 #
       
    21 
       
    22 #
       
    23 # Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
       
    24 #
       
    25 
       
    26 Puppet::Type.type(:address_object).provide(:address_object) do
       
    27     desc "Provider for creating Oracle Solaris address objects"
       
    28     confine :operatingsystem => [:solaris]
       
    29     defaultfor :osfamily => :solaris, :kernelrelease => ['5.11', '5.12']
       
    30     commands :ipadm => '/usr/sbin/ipadm'
       
    31 
       
    32     def self.instances
       
    33         ipadm("show-addr", "-p", "-o", "addrobj,type,state,addr").split(
       
    34               "\n").collect do |line|
       
    35             addrobj, type, state, addr = line.split(":", 4)
       
    36 
       
    37             # replace any hypen with an underscore
       
    38             type = type.gsub(/\-/, "_")
       
    39 
       
    40             # look to see if this is a point-to-point address object
       
    41             if addr.include?("->")
       
    42                 local, remote = addr.split("->")
       
    43                 local = locale.delete("\\")
       
    44                 remote = remote.delete("\\")
       
    45             elsif type.downcase == "dhcp" and addr == "?"
       
    46                 local = nil
       
    47                 remote = nil
       
    48             else
       
    49                 local = addr.delete("\\")
       
    50                 remote = nil
       
    51             end
       
    52 
       
    53             if state == "ok"
       
    54                 enable = :true
       
    55             else
       
    56                 enable = :false
       
    57             end
       
    58 
       
    59             new(:name => addrobj,
       
    60                 :ensure => :present,
       
    61                 :address_type => type,
       
    62                 :enable => enable,
       
    63                 :address => local,
       
    64                 :remote_address => remote)
       
    65         end
       
    66     end
       
    67 
       
    68     def self.prefetch(resources)
       
    69         # pull the instances on the system
       
    70         addrobjs = instances
       
    71 
       
    72         # set the provider for the resource to set the property_hash
       
    73         resources.keys.each do |name|
       
    74             if provider = addrobjs.find{ |addrobj| addrobj.name == name}
       
    75                 resources[name].provider = provider
       
    76             end
       
    77         end
       
    78     end
       
    79 
       
    80     # property getters
       
    81     Puppet::Type.type(:address_object).validproperties.each do |field|
       
    82         next if field == :ensure
       
    83         define_method(field) do
       
    84             @property_hash[field]
       
    85         end
       
    86     end
       
    87 
       
    88     def add_options
       
    89         options = []
       
    90         if @resource[:temporary] == :true
       
    91             options << "-t"
       
    92         end
       
    93 
       
    94         if address_type = @resource[:address_type]
       
    95             options << "-T" << address_type
       
    96         end
       
    97 
       
    98         if address = @resource[:address]
       
    99             options << "-a" << "local=#{address}"
       
   100         end
       
   101 
       
   102         if remote_address = @resource[:remote_address]
       
   103             options << "-a" << "remote=#{remote_address}"
       
   104         end
       
   105 
       
   106         if down = @resource[:down]
       
   107             options << "-d" << down
       
   108         end
       
   109 
       
   110         if seconds = @resource[:seconds]
       
   111             options << "-w" << seconds
       
   112         end
       
   113 
       
   114         if hostname = @resource[:hostname]
       
   115             options << "-h" << hostname
       
   116         end
       
   117 
       
   118         if interface_id = @resource[:interface_id]
       
   119             options << "-i" << "local=#{interface_id}"
       
   120         end
       
   121 
       
   122         if remote_interface_id = @resource[:remote_interface_id]
       
   123             options << "-i" << "remote=#{remote_interface_id}"
       
   124         end
       
   125 
       
   126         if stateful = @resource[:stateful]
       
   127             options << "-p" << "stateful=#{stateful}"
       
   128         end
       
   129 
       
   130         if stateless = @resource[:stateless]
       
   131             options << "-p" << "stateless=#{stateless}"
       
   132         end
       
   133         options
       
   134     end
       
   135 
       
   136     def exists?
       
   137         @property_hash[:ensure] == :present
       
   138     end
       
   139 
       
   140     def create
       
   141         ipadm("create-addr", add_options, @resource[:name])
       
   142         if @resource[:enable] == :true
       
   143             ipadm("enable-addr", @resource[:name])
       
   144         end
       
   145     end
       
   146 
       
   147     def destroy
       
   148         ipadm("delete-addr", @resource[:name])
       
   149     end
       
   150 end