Skip to content

Commit 7b662cc

Browse files
committed
fix: bucket config
1 parent 6ff082f commit 7b662cc

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

influxdb/buckets.sls

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
jq:
55
pkg.installed
66
7+
{% if "remote" in influxdb %}
8+
{%- set base_url = "https://" ~ influxdb['remote']['host'] ~ ":" ~ influxdb['remote']['port'] %}
9+
{% endif %}
10+
711
{%- if "bucket" in influxdb %}
812
{%- for config in influxdb["bucket"] %}
913
{% if "remote" not in influxdb %}
@@ -19,32 +23,33 @@ influxdb_bucket_{{ config['name'] }}:
1923
- pkg: jq
2024
{%- set bucket = salt['cmd.shell']("influx bucket list --json | jq -r '.[] | select(.name == \"" + config['name'] + "\").id'") %}
2125
{% else %}
26+
{%- set orgID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/orgs' | jq -r '.orgs[0].id'") %}
27+
2228
get_bucket_{{ config['name'] }}:
2329
http.query:
24-
- name: 'https://{{ influxdb['remote']['host'] }}/api/v2/buckets/{{ config['name'] }}'
30+
- name: '{{ base_url }}/api/v2/buckets?orgId={{ orgID }}&name={{ config['name'] }}'
2531
- status: 200
2632
- method: GET
2733
- header_dict:
2834
Authorization: Token {{ influxdb['user']['admin']['token'] }}
2935
3036
{%- set bucket_data = {
31-
name: config['name'],
32-
description: config['description'] | default('A bucket for ' + config['name']),
33-
orgID: config['orgID'],
34-
retentionRules: [],
35-
rp: config['retention_policy']
37+
'name': config['name'],
38+
'description': config['description'] | default('A bucket for ' + config['name']),
39+
'orgID': orgID,
40+
'rp': config['retention_policy'] | default('0'),
3641
} %}
3742
create_bucket_{{ config['name'] }}:
3843
http.query:
39-
- name: 'https://{{ influxdb['remote']['host'] }}/api/v2/buckets'
40-
- status: 200
44+
- name: '{{ base_url }}/api/v2/buckets'
45+
- status: 201
4146
- method: POST
4247
- data: '{{ bucket_data | tojson }}'
4348
- header_dict:
4449
Authorization: Token {{ influxdb['user']['admin']['token'] }}
4550
- onfail:
4651
- http: get_bucket_{{ config['name'] }}
47-
{%- set bucket = salt['cmd.shell']("curl -s -f -H'Authorization: Token {{ influxdb['user']['admin']['token'] }}' https://{{ influxdb['remote']['host'] }}/api/v2/buckets | jq -r '.[] | select(.name == \"" + config['name'] + "\").id'") %}
52+
{%- set bucket = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/buckets' | jq -r '.buckets[] | select(.name == \"" + config['name'] + "\").id'") %}
4853
{% endif %}
4954
5055
{%- if 'mapping' in config and bucket %}
@@ -61,10 +66,30 @@ influxdb_bucket_{{ config['name'] }}_mapping_{{ dbrp_config['db'] }}/{{ dbrp_con
6166
- require:
6267
- cmd: influxdb_bucket_{{ config['name'] }}
6368
{%- else %}
64-
influxdb_bucket_{{ config['name'] }}_mapping_{{ dbrp_config['db'] }}/{{ dbrp_config['rp'] }}:
65-
cmd.run:
66-
- name: >
67-
echo "{{ bucket }}"
69+
get_bucket_{{ config['name'] }}_mapping_{{ dbrp_config['db'] }}/{{ dbrp_config['rp'] }}:
70+
http.query:
71+
- name: '{{ base_url }}/api/v2/dbrps?orgID={{ orgID }}&bucketID={{ bucket }}&db={{ dbrp_config['db'] }}&rp={{ dbrp_config['rp'] }}'
72+
- status: 200
73+
- method: GET
74+
- header_dict:
75+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
76+
77+
{%- set dbrp_data = {
78+
'bucketID': bucket,
79+
'database': dbrp_config['db'],
80+
'retention_policy': dbrp_config['rp'] | default('autogen'),
81+
'orgID': orgID,
82+
} %}
83+
create_bucket_{{ config['name'] }}_mapping_{{ dbrp_config['db'] }}/{{ dbrp_config['rp'] }}:
84+
http.query:
85+
- name: '{{ base_url }}/api/v2/dbrps'
86+
- status: 201
87+
- method: POST
88+
- data: '{{ dbrp_data | tojson }}'
89+
- header_dict:
90+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
91+
- onfail:
92+
- http: get_bucket_{{ config['name'] }}_mapping_{{ dbrp_config['db'] }}/{{ dbrp_config['rp'] }}
6893
{%- endif %}
6994
{%- endfor %}
7095
{%- endif %}

influxdb/users.sls

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,64 @@ influxdb_user_{{ loop.index0 }}:
1717
{% endif %}
1818
1919
{% if "user" in influxdb and "remote" in influxdb %}
20+
{%- set base_url = "https://" ~ influxdb['remote']['host'] ~ ":" ~ influxdb['remote']['port'] %}
2021
{% for name,config in influxdb["user"].items() %}
2122
get_user_{{ loop.index0 }}:
2223
http.query:
23-
- name: 'https://{{ influxdb['remote']['host'] }}/api/v2/users/{{ name }}'
24+
- name: '{{ base_url }}/api/v2/users/?name={{ name }}'
2425
- status: 200
2526
- method: GET
2627
- header_dict:
2728
Authorization: Token {{ influxdb['user']['admin']['token'] }}
2829
2930
create_user_{{ loop.index0 }}:
3031
http.query:
31-
- name: 'https://{{ influxdb['remote']['host'] }}/api/v2/users'
32-
- status: 200
32+
- name: '{{ base_url }}/api/v2/users'
33+
- status: 201
3334
- method: POST
3435
- data: '{"name": "{{ name }}"}'
3536
- header_dict:
3637
Authorization: Token {{ influxdb['user']['admin']['token'] }}
3738
- onfail:
3839
- http: get_user_{{ loop.index0 }}
3940
41+
{%- set id = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/users?name=" ~ name ~ "' | jq -r '.users[0].id'") %}
4042
{% if "password" in config %}
4143
set_password_{{ loop.index0 }}:
4244
http.query:
43-
- name: 'https://{{ influxdb['remote']['host'] }}/api/v2/users{{ name }}/password'
44-
- status: 200
45+
- name: '{{ base_url }}/api/v2/users/{{ id }}/password'
46+
- status: 204
4547
- method: POST
4648
- data: '{"password": "{{ config["password"] }}"}'
4749
- header_dict:
4850
Authorization: Token {{ influxdb['user']['admin']['token'] }}
4951
{% endif %}
5052
53+
{% if "grants" in config %}
54+
{%- for bucket,access in config['grants'].items() %}
55+
{%- set bucketID = salt['cmd.shell']("curl -s -f -H'Authorization: Token " ~ influxdb['user']['admin']['token'] ~ "' '" ~ base_url ~ "/api/v2/buckets' | jq -r '.buckets[] | select(.name == \"" + config['name'] + "\").id'") %}
56+
check_grant_user_{{ name }}_to_{{ bucket }}:
57+
http.query:
58+
- name: '{{ base_url }}/api/v2/buckets/{{ bucketID }}/members'
59+
- status: 200
60+
- method: GET
61+
- match: '"{{ name }}"'
62+
- match_type: string
63+
- header_dict:
64+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
65+
66+
grant_user_{{ name }}_to_{{ bucket }}:
67+
http.query:
68+
- name: '{{ base_url }}/api/v2/buckets/{{ bucketID }}/members'
69+
- status: 201
70+
- method: POST
71+
- data: '{"name": "{{ name }}", "id": "{{ id }}"}'
72+
- header_dict:
73+
Authorization: Token {{ influxdb['user']['admin']['token'] }}
74+
- onfail:
75+
- http: check_grant_user_{{ name }}_to_{{ bucket }}
76+
{%- endfor %}
77+
{% endif %}
78+
5179
{% endfor %}
5280
{% endif %}

0 commit comments

Comments
 (0)