44jq:
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+
2228get_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} % }
3742create_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 % }
0 commit comments