components/ruby/puppet-modules/oracle-solaris_providers/files/etc/puppet/modules/solaris_providers/lib/puppet/provider/address_object/solaris.rb
changeset 5438 c068f8c677e8
parent 5093 b3ada587e28f
equal deleted inserted replaced
5437:449f3459d285 5438:c068f8c677e8
       
     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, 2015, 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 = local.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             down = :false
       
    54             if state == "ok"
       
    55                 enable = :true
       
    56             elsif state == "disabled"
       
    57                 enable = :false
       
    58             elsif state == "down"
       
    59                 down = :true
       
    60                 enable = :true
       
    61             end
       
    62 
       
    63             new(:name => addrobj,
       
    64                 :ensure => :present,
       
    65                 :address_type => type,
       
    66                 :down => down,
       
    67                 :enable => enable,
       
    68                 :address => local,
       
    69                 :remote_address => remote)
       
    70         end
       
    71     end
       
    72 
       
    73     def self.prefetch(resources)
       
    74         # pull the instances on the system
       
    75         addrobjs = instances
       
    76 
       
    77         # set the provider for the resource to set the property_hash
       
    78         resources.keys.each do |name|
       
    79             if provider = addrobjs.find{ |addrobj| addrobj.name == name}
       
    80                 resources[name].provider = provider
       
    81             end
       
    82         end
       
    83     end
       
    84 
       
    85     # property getters
       
    86     Puppet::Type.type(:address_object).validproperties.each do |field|
       
    87         next if field == :ensure
       
    88         define_method(field) do
       
    89             @property_hash[field]
       
    90         end
       
    91     end
       
    92 
       
    93     def enable=(value)
       
    94         if value == :true
       
    95             ipadm("enable-addr", "-t", @resource[:name])
       
    96         elsif value == :false
       
    97             ipadm("disable-addr", "-t", @resource[:name])
       
    98         end
       
    99     end
       
   100     
       
   101     def is_temp
       
   102         temp = []
       
   103         if @resource[:temporary] == :true
       
   104             temp << "-t"
       
   105         end
       
   106         temp
       
   107     end
       
   108     
       
   109     def down=(value)
       
   110         if value == :true
       
   111             ipadm("down-addr", is_temp, @resource[:name])
       
   112         elsif value == :false
       
   113             ipadm("up-addr", is_temp, @resource[:name])
       
   114         end
       
   115     end
       
   116             
       
   117     def add_options
       
   118         options = []
       
   119         if @resource[:temporary] == :true
       
   120             options << "-t"
       
   121         end
       
   122 
       
   123         if address_type = @resource[:address_type]
       
   124             options << "-T" << address_type
       
   125         end
       
   126 
       
   127         if address = @resource[:address]
       
   128             options << "-a" << "local=#{address}"
       
   129         end
       
   130 
       
   131         if remote_address = @resource[:remote_address]
       
   132             options << "-a" << "remote=#{remote_address}"
       
   133         end
       
   134 
       
   135         if @resource[:down] == :true
       
   136             options << "-d"
       
   137         end
       
   138 
       
   139         if seconds = @resource[:seconds]
       
   140             options << "-w" << seconds
       
   141         end
       
   142 
       
   143         if hostname = @resource[:hostname]
       
   144             options << "-h" << hostname
       
   145         end
       
   146 
       
   147         if interface_id = @resource[:interface_id]
       
   148             options << "-i" << "local=#{interface_id}"
       
   149         end
       
   150 
       
   151         if remote_interface_id = @resource[:remote_interface_id]
       
   152             options << "-i" << "remote=#{remote_interface_id}"
       
   153         end
       
   154 
       
   155         if stateful = @resource[:stateful]
       
   156             options << "-p" << "stateful=#{stateful}"
       
   157         end
       
   158 
       
   159         if stateless = @resource[:stateless]
       
   160             options << "-p" << "stateless=#{stateless}"
       
   161         end
       
   162         options
       
   163     end
       
   164 
       
   165     def exists?
       
   166         @property_hash[:ensure] == :present
       
   167     end
       
   168 
       
   169     def create
       
   170         ipadm("create-addr", add_options, @resource[:name])
       
   171     end
       
   172 
       
   173     def destroy
       
   174         ipadm("delete-addr", @resource[:name])
       
   175     end
       
   176 end