Repository navigation
Expand file tree
/
Copy pathintegration.nix
More file actions
108 lines (95 loc) · 3.37 KB
/
Copy pathintegration.nix
File metadata and controls
108 lines (95 loc) · 3.37 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{ lib, ... }:
with builtins;
rec {
/**
Uppercase the first character of `s`, leaving the rest unchanged. An
empty string is returned as-is.
# Arguments
`s` (`String`): The string to capitalize
# Example
```nix
capitalize "sonarr"
=> "Sonarr"
```
*/
capitalize = s: if s == "" then s else lib.toUpper (substring 0 1 s) + substring 1 (-1) s;
/**
Build a shell script that idempotently registers an integration with
an *Arr-stack app's (Sonarr/Radarr/Prowlarr-style) REST API: it checks
`apiEndpoint` for an existing entry matching
`settings.implementation`, and if none is found, `POST`s `settings` to
create one.
# Arguments
`name` (`String`): Name of the app being integrated with, used in log
messages
`apiEndpoint` (`String`): URL of the integration list/create endpoint
`authApiKeyVar` (`String`): Environment variable holding the API key
for `apiEndpoint`
`settings` (`AttrSet`): The integration payload to `POST`;
`settings.fields` is an attrset serialized to the API's field-list
format, and `settings.implementation` identifies the integration type
`apiKeyFieldName` (`String`): Field name to inject `apiKeyVar`'s value
under in `settings.fields` (*optional*, required if `apiKeyVar` is set)
`apiKeyVar` (`String`): Environment variable to inject into
`settings.fields` (*optional*)
# Example
```nix
integration.mkArrStackIntegrationScript {
name = "Sonarr";
apiEndpoint = "http://prowlarr.prowlarr:9696/api/v1/applications";
authApiKeyVar = "PROWLARR_API_KEY";
apiKeyFieldName = "apiKey";
apiKeyVar = "SONARR_API_KEY";
settings = {
implementation = "Sonarr";
fields.baseUrl = "http://sonarr.sonarr:8989";
};
}
```
*/
mkArrStackIntegrationScript =
{
name,
apiEndpoint,
authApiKeyVar,
settings,
apiKeyFieldName ? null,
apiKeyVar ? null,
}:
let
title = capitalize name;
in
''
set -eo pipefail
data=$(curl -sfX GET '${apiEndpoint}' \
-H 'Content-Type: application/json' \
-H "X-Api-Key: ''${${authApiKeyVar}:?}")
payload='${builtins.toJSON (settings // { fields = lib.attrsToList settings.fields; })}'
${lib.optionalString (apiKeyVar != null) ''
payload=$(jq --arg ${apiKeyVar} "''${${apiKeyVar}:?}" '.fields+=[{"name":"${apiKeyFieldName}","value":''$${apiKeyVar}}]' <<<"$payload")
''}
if id=$(jq -re '.[] | select(.implementation == "${settings.implementation}") | .id' <<<"$data"); then
echo "${title} integration already set up, updating settings" >&2
if curl -sfX PUT "${apiEndpoint}/$id" \
-H 'Content-Type: application/json' \
-H "X-Api-Key: ''${${authApiKeyVar}:?}" \
-d "$payload"; then
echo "Successfully updated ${title} integration" >&2
else
echo "${title} integration update failed" >&2
exit 1
fi
else
echo "Setting up ${title} integration" >&2
if curl -sfX POST '${apiEndpoint}' \
-H 'Content-Type: application/json' \
-H "X-Api-Key: ''${${authApiKeyVar}:?}" \
-d "$payload"; then
echo "Successfully set up ${title} integration" >&2
else
echo "${title} integration setup failed" >&2
exit 1
fi
fi
'';
}