summaryrefslogtreecommitdiff
path: root/lib/chef/resource/windows_dfs_folder.rb
blob: 8078f965fb053139ca400d3e5bc7aead303a270d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#
# Author:: Jason Field
#
# Copyright:: 2018, Calastone Ltd.
# Copyright:: 2019, Chef Software, Inc.
#
# 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_relative "../resource"

class Chef
  class Resource
    class WindowsDfsFolder < Chef::Resource
      resource_name :windows_dfs_folder
      provides :windows_dfs_folder

      description "The windows_dfs_folder resources creates a folder within dfs as many levels deep as required."
      introduced "15.0"

      property :folder_path, String,
        description: "An optional property to set the path of the dfs folder if it differs from the resource block's name.",
        name_property: true

      property :namespace_name, String,
        description: "The namespace this should be created within.",
        required: true

      property :target_path, String,
        description: "The target that this path will connect you to."

      property :description, String,
        description: "Description for the share."

      action :create do
        description "Creates the folder in dfs namespace."

        raise "target_path is required for install" unless property_is_set?(:target_path)
        raise "description is required for install" unless property_is_set?(:description)

        powershell_script "Create or Update DFS Folder" do
          code <<-EOH

            $needs_creating = (Get-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' -ErrorAction SilentlyContinue) -eq $null
            if (!($needs_creating))
            {
              Remove-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' -Force
            }
              New-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' -TargetPath '#{new_resource.target_path}' -Description '#{new_resource.description}'
          EOH
          not_if "return ((Get-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' -ErrorAction SilentlyContinue).Description -eq '#{new_resource.description}' -and  (Get-DfsnFolderTarget -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}').TargetPath -eq '#{new_resource.target_path}' )"
        end
      end

      action :delete do
        description "Deletes the folder in the dfs namespace."

        powershell_script "Delete DFS Namespace" do
          code <<-EOH
            Remove-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' -Force
          EOH
          only_if "return ((Get-DfsnFolder -Path '\\\\#{ENV["COMPUTERNAME"]}\\#{new_resource.namespace_name}\\#{new_resource.folder_path}' ) -ne $null)"
        end
      end
    end
  end
end