-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# @summary Google Drive confguration for Rclone. | ||
# | ||
# Ensures Drive Rclone configuration of given name and params. Include of `rclone` is required. | ||
# Support only service account credentials (token authentication requires human interaction when setup). | ||
# | ||
# @example | ||
# rclone::config::gdrive { 'drive_remote': | ||
# os_user => 'my_user', | ||
# client_id => 'SOME_CLIENT_ID', | ||
# client_secret => 'SOME_CLIENT_SECRET', | ||
# service_account_credentials => 'SERVICE_CREDENTIALS', | ||
# } | ||
# | ||
# @param ensure | ||
# configuration ensure | ||
# @param os_user | ||
# operating system user - used to execute rclone commands, effective configuration owner | ||
# @param config_name | ||
# configuration name - should be unique among Rclone configurations, defaults to title | ||
# @param client_id | ||
# Google drive client_id, maps to Rclone `client_id` property | ||
# @param client_secret | ||
# Google drive client_secret, maps to Rclone `client_secret` property | ||
# @param service_account_credentials | ||
# Google drive service_account_credentials, maps to Rclone `service_account_credentials` property | ||
# @param scope | ||
# Google drive access scope, maps to Rclone `scope` property | ||
# @param root_folder_id | ||
# Id of the drive root folder, maps to Rclone `root_folder_id` property | ||
# @param team_drive | ||
# Id of the team drive, maps to Rclone `team_drive` property | ||
define rclone::config::gdrive ( | ||
String $os_user, | ||
String $client_id, | ||
String $client_secret, | ||
String $service_account_credentials, | ||
Enum['present', 'absent'] $ensure = 'present', | ||
String $config_name = $title, | ||
String $scope = 'drive', | ||
Optional[String] $root_folder_id = undef, | ||
Optional[String] $team_drive = undef, | ||
) { | ||
|
||
if ! defined(Class[rclone]) { | ||
fail('You must include the rclone base class before using any defined resources') | ||
} | ||
|
||
$_rclone_exec_defaults = { | ||
user => $os_user, | ||
path => '/usr/bin', | ||
require => Class[rclone], | ||
} | ||
|
||
case $ensure { | ||
'present': { | ||
|
||
$_options = { | ||
root_folder_id => $root_folder_id, | ||
team_drive => $team_drive, | ||
} | ||
.filter |$key, $val| { $val != undef }.map |$key, $val| { "${key} ${val}" }.join(' ') | ||
|
||
exec { default: *=> $_rclone_exec_defaults; "rclone create remote ${config_name} for user ${os_user}": | ||
command => @("CMD") | ||
rclone config create ${config_name} drive \ | ||
client_id ${client_id} client_secret ${client_secret} service_account_credentials ${service_account_credentials} scope ${scope} \ | ||
${_options} | ||
| CMD | ||
} | ||
} | ||
|
||
'absent': { | ||
exec { default: *=> $_rclone_exec_defaults; "rclone delete remote ${config_name} for user ${os_user}": | ||
command => "rclone config delete ${config_name}", | ||
} | ||
} | ||
|
||
default: { | ||
fail("Invalid ensure value '${ensure}'") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'rclone::config::gdrive' do | ||
let(:title) { 'test_r' } | ||
let(:params) do | ||
{ | ||
os_user: 'user', | ||
client_id: 'gdrive_id', | ||
client_secret: 'gdrive_secret', | ||
service_account_credentials: 'SAC', | ||
} | ||
end | ||
|
||
context 'on missing rclone main class' do | ||
it { is_expected.to compile.and_raise_error(%r{include the rclone base class}) } | ||
end | ||
|
||
on_supported_os.each do |os, os_facts| | ||
context "on #{os}" do | ||
let(:facts) { os_facts } | ||
let(:pre_condition) { 'include rclone' } | ||
|
||
it { is_expected.to compile } | ||
|
||
it { | ||
is_expected.to contain_exec('rclone create remote test_r for user user') | ||
.with( | ||
command: 'rclone config create test_r drive \\' \ | ||
"\nclient_id gdrive_id client_secret gdrive_secret service_account_credentials SAC scope drive \\\n\n", | ||
user: 'user', | ||
path: '/usr/bin', | ||
) | ||
.that_requires('Class[rclone]') | ||
} | ||
|
||
context 'with optional params' do | ||
let(:params) do | ||
super().merge( | ||
root_folder_id: 'root_id', | ||
team_drive: 'team_id', | ||
) | ||
end | ||
|
||
it { | ||
is_expected.to contain_exec('rclone create remote test_r for user user') | ||
.with( | ||
command: 'rclone config create test_r drive \\' \ | ||
"\nclient_id gdrive_id client_secret gdrive_secret service_account_credentials SAC scope drive \\" \ | ||
"\nroot_folder_id root_id team_drive team_id\n", | ||
user: 'user', | ||
path: '/usr/bin', | ||
) | ||
.that_requires('Class[rclone]') | ||
} | ||
end | ||
|
||
context 'with ensure => absent' do | ||
let(:params) do | ||
super().merge(ensure: 'absent') | ||
end | ||
|
||
it { | ||
is_expected.to contain_exec('rclone delete remote test_r for user user') | ||
.with( | ||
command: 'rclone config delete test_r', | ||
user: 'user', | ||
path: '/usr/bin', | ||
) | ||
.that_requires('Class[rclone]') | ||
} | ||
end | ||
end | ||
end | ||
end |