Merge ~jocave/plainbox-provider-resource:attrs-of-interfaces into plainbox-provider-resource:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Sylvain Pineau
Approved revision: e4df2aa8c1563e9588812cbbcb99560b11419591
Merged at revision: b55b87c413154300af99347e2d412b43a9cd3611
Proposed branch: ~jocave/plainbox-provider-resource:attrs-of-interfaces
Merge into: plainbox-provider-resource:master
Diff against target: 49 lines (+15/-0)
1 file modified
bin/snapd_resource (+15/-0)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+325731@code.launchpad.net

Description of the change

Add the attributes of an interface to the output of "snapd_resource interfaces endpoints".

The attributes will be keyed like attr_<slugified-name>.

Tested to work on classic and Caracalla

To post a comment you must log in.
Revision history for this message
Jonathan Cave (jocave) wrote :

Going to modify the comment in slugify function

Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Looks good, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bin/snapd_resource b/bin/snapd_resource
2index ca0ad37..157a834 100755
3--- a/bin/snapd_resource
4+++ b/bin/snapd_resource
5@@ -9,6 +9,7 @@ import json
6 import io
7 import logging
8 import os
9+import string
10 import sys
11 import time
12
13@@ -124,6 +125,13 @@ class InterfacesQuery(SnapdQuery):
14 prefix = '/v2/interfaces'
15
16
17+def slugify(_string):
18+ """Transform string to one that can be used as the key in a resource job"""
19+ valid_chars = frozenset(
20+ "_{}{}".format(string.ascii_letters, string.digits))
21+ return ''.join(c if c in valid_chars else '_' for c in _string)
22+
23+
24 class Endpoints(InterfacesQuery):
25
26 def invoked(self, ctx):
27@@ -139,6 +147,9 @@ class Endpoints(InterfacesQuery):
28 print_field(f)
29 print('type: plug')
30 print('name: {}'.format(plug['plug']))
31+ if 'attrs' in plug:
32+ for attr, val in plug['attrs'].items():
33+ print('attr_{}: {}'.format(slugify(attr), val))
34 print()
35
36 for slot in data['result']['slots']:
37@@ -151,8 +162,12 @@ class Endpoints(InterfacesQuery):
38 print_field(f)
39 print('type: slot')
40 print('name: {}'.format(slot['slot']))
41+ if 'attrs' in slot:
42+ for attr, val in slot['attrs'].items():
43+ print('attr_{}: {}'.format(slugify(attr), val))
44 print()
45
46+
47 Connection = namedtuple(
48 'Connection',
49 ['target_snap', 'target_slot', 'plug_snap', 'plug_plug'])

Subscribers

People subscribed via source and target branches