Skip to content

Commit

Permalink
Introduce gdrive config
Browse files Browse the repository at this point in the history
  • Loading branch information
jimirocks committed Apr 20, 2020
1 parent 49e5da5 commit 251a9f3
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 0 deletions.
87 changes: 87 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _Private Classes_

**Defined types**

* [`rclone::config::gdrive`](#rcloneconfiggdrive): Google Drive confguration for Rclone.
* [`rclone::config::s3`](#rcloneconfigs3): S3 confguration for Rclone.

**Functions**
Expand Down Expand Up @@ -55,6 +56,92 @@ Default value: 'latest'

## Defined types

### rclone::config::gdrive

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).

#### Examples

#####

```puppet
rclone::config::gdrive { 'drive_remote':
os_user => 'my_user',
client_id => 'SOME_CLIENT_ID',
client_secret => 'SOME_CLIENT_SECRET',
service_account_credentials => 'SERVICE_CREDENTIALS',
}
```

#### Parameters

The following parameters are available in the `rclone::config::gdrive` defined type.

##### `ensure`

Data type: `Enum['present', 'absent']`

configuration ensure

Default value: 'present'

##### `os_user`

Data type: `String`

operating system user - used to execute rclone commands, effective configuration owner

##### `config_name`

Data type: `String`

configuration name - should be unique among Rclone configurations, defaults to title

Default value: $title

##### `client_id`

Data type: `String`

Google drive client_id, maps to Rclone `client_id` property

##### `client_secret`

Data type: `String`

Google drive client_secret, maps to Rclone `client_secret` property

##### `service_account_credentials`

Data type: `String`

Google drive service_account_credentials, maps to Rclone `service_account_credentials` property

##### `scope`

Data type: `String`

Google drive access scope, maps to Rclone `scope` property

Default value: 'drive'

##### `root_folder_id`

Data type: `Optional[String]`

Id of the drive root folder, maps to Rclone `root_folder_id` property

Default value: `undef`

##### `team_drive`

Data type: `Optional[String]`

Id of the team drive, maps to Rclone `team_drive` property

Default value: `undef`

### rclone::config::s3

Ensures S3 Rclone configuration of given name and params. Include of `rclone` is required.
Expand Down
83 changes: 83 additions & 0 deletions manifests/config/gdrive.pp
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}'")
}
}

}
76 changes: 76 additions & 0 deletions spec/defines/config/gdrive_spec.rb
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

0 comments on commit 251a9f3

Please sign in to comment.