components/ruby/puppet/files/solaris/lib/puppet/provider/vnic/solaris.rb
changeset 5438 c068f8c677e8
parent 5437 449f3459d285
child 5439 a006e5bb8577
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, 2014, Oracle and/or its affiliates. All rights reserved.
       
    24 #
       
    25 
       
    26 Puppet::Type.type(:vnic).provide(:vnic) do
       
    27     desc "Provider for creating VNICs in the Solaris OS"
       
    28     confine :operatingsystem => [:solaris]
       
    29     defaultfor :osfamily => :solaris, :kernelrelease => ['5.11', '5.12']
       
    30     commands :dladm => '/usr/sbin/dladm'
       
    31 
       
    32     def self.instances
       
    33         vnics = []
       
    34         dladm("show-vnic", "-p", "-o", "link,over,macaddress").split(
       
    35               "\n").collect do |line|
       
    36             link, over, mac = line.split(":", 3)
       
    37             next if not link =~ /^[[:alpha:]]([\w.]){1,29}([\d])$/i
       
    38             # remove the escape character
       
    39             vnics << new(:name => link,
       
    40                          :ensure => :present,
       
    41                          :lower_link => over,
       
    42                          :mac_address => mac.delete("\\"))
       
    43         end
       
    44         vnics
       
    45     end
       
    46         
       
    47     def self.prefetch(resources)
       
    48         # pull the instances on the system
       
    49         vnics = instances
       
    50 
       
    51         # set the provider for the resource to set the property_hash
       
    52         resources.keys.each do |name|
       
    53             if provider = vnics.find{ |vnic| vnic.name == name}
       
    54                 resources[name].provider = provider
       
    55             end
       
    56         end
       
    57     end
       
    58 
       
    59     def lower_link
       
    60         @property_hash[:lower_link]
       
    61     end
       
    62 
       
    63     def lower_link=(value)
       
    64         dladm("modify-vnic", "-l", value, @resource[:name])
       
    65     end
       
    66 
       
    67     def mac_address
       
    68         @property_hash[:mac_address]
       
    69     end
       
    70 
       
    71     def mac_address=(value)
       
    72         dladm("modify-vnic", "-m", value, @resource[:name])
       
    73     end
       
    74 
       
    75     def add_options
       
    76        options = []
       
    77        if @resource[:temporary] == :true
       
    78          options << "-t"
       
    79        end
       
    80 
       
    81        if @resource[:mac_address]
       
    82            options << "-m" << @resource[:mac_address]
       
    83        end
       
    84        options
       
    85     end
       
    86 
       
    87     def exists?
       
    88         @property_hash[:ensure] == :present
       
    89     end
       
    90 
       
    91     def create
       
    92         dladm('create-vnic', '-l', @resource[:lower_link], add_options,
       
    93               @resource[:name])
       
    94     end
       
    95 
       
    96     def destroy
       
    97         dladm('delete-vnic', @resource[:name])
       
    98     end
       
    99 end