28
28
import java .util .HashSet ;
29
29
import java .util .List ;
30
30
import java .util .Map ;
31
+ import java .util .Optional ;
31
32
import java .util .Set ;
32
33
import java .util .stream .Collectors ;
33
34
import okhttp3 .Call ;
@@ -68,27 +69,18 @@ public Set<APIResource> findAll(
68
69
String group , List <String > versions , String preferredVersion , String path )
69
70
throws ApiException {
70
71
V1APIResourceList resourceList = resourceDiscovery (path );
71
- return sort (group , versions , preferredVersion , resourceList );
72
+ return groupResourcesByName (group , versions , preferredVersion , resourceList );
72
73
}
73
74
74
- private Set <APIResource > sort (
75
- String group , List <String > versions , String preferredVersion , V1APIResourceList resourceList )
76
- throws ApiException {
77
- Map <String , Set <String >> subResources = new HashMap <>();
75
+ public Set <APIResource > groupResourcesByName (
76
+ String group ,
77
+ List <String > versions ,
78
+ String preferredVersion ,
79
+ V1APIResourceList resourceList ) {
80
+ // parse raw discovery responses to APIResource for better readability
78
81
Set <APIResource > resources =
79
82
resourceList .getResources ().stream ()
80
- .filter (
81
- r -> {
82
- boolean isSubResource = r .getName ().contains ("/" );
83
- if (isSubResource ) {
84
- String [] parts = r .getName ().split ("/" , 2 );
85
- if (!subResources .containsKey (parts [0 ])) {
86
- subResources .put (parts [0 ], new HashSet <>());
87
- }
88
- subResources .get (parts [0 ]).add (parts [1 ]);
89
- }
90
- return !isSubResource ;
91
- })
83
+ .filter (r -> !getSubResourceNameIfPossible (r .getName ()).isPresent ())
92
84
.map (
93
85
r ->
94
86
new APIResource (
@@ -100,6 +92,9 @@ private Set<APIResource> sort(
100
92
r .getName (),
101
93
r .getSingularName ()))
102
94
.collect (Collectors .toSet ());
95
+
96
+ // wiring up connections between major-resource and sub-resources
97
+ Map <String , Set <String >> subResources = manageRelationFromResourceToSubResources (resourceList );
103
98
resources .stream ()
104
99
.forEach (
105
100
r -> {
@@ -110,6 +105,37 @@ private Set<APIResource> sort(
110
105
return resources ;
111
106
}
112
107
108
+ private Map <String , Set <String >> manageRelationFromResourceToSubResources (
109
+ V1APIResourceList resourceList ) {
110
+ Map <String , Set <String >> subResources = new HashMap <>();
111
+ resourceList .getResources ().stream ()
112
+ .forEach (r -> subResources .put (r .getName (), new HashSet <>()));
113
+ resourceList .getResources ().stream ()
114
+ .forEach (
115
+ r -> {
116
+ getSubResourceNameIfPossible (r .getName ())
117
+ .ifPresent (
118
+ subResourceName -> {
119
+ subResources .get (getMajorResourceName (r .getName ())).add (subResourceName );
120
+ });
121
+ });
122
+ return subResources ;
123
+ }
124
+
125
+ private String getMajorResourceName (String discoveredResourceName ) {
126
+ String [] parts = discoveredResourceName .split ("/" , 2 );
127
+ return parts [0 ];
128
+ }
129
+
130
+ private Optional <String > getSubResourceNameIfPossible (String discoveredResourceName ) {
131
+ boolean isSubResource = discoveredResourceName .contains ("/" );
132
+ if (!isSubResource ) {
133
+ return Optional .empty ();
134
+ }
135
+ String [] parts = discoveredResourceName .split ("/" , 2 );
136
+ return Optional .of (parts [1 ]);
137
+ }
138
+
113
139
public V1APIVersions legacyCoreApi () throws ApiException {
114
140
return versionDiscovery ("/api" );
115
141
}
0 commit comments