2222from enum import Enum
2323from inspect import FrameInfo
2424from json import JSONDecodeError
25- from typing import Dict , List , Callable , Iterable , TypeVar , Optional , Union
25+ from typing import Callable , Dict , Iterable , List , Optional , TypeVar , Union
2626
27+ from openstack_cli .modules .apputils .curl import CURLResponse , CurlRequestType , curl
28+ from openstack_cli .modules .apputils .progressbar import CharacterStyles , ProgressBar , ProgressBarFormat , \
29+ ProgressBarOptions
2730from openstack_cli .modules .apputils .terminal .colors import Colors
28- from openstack_cli .modules .apputils .curl import curl , CurlRequestType , CURLResponse
29- from openstack_cli .modules .openstack .api_objects import ComputeLimits , VolumeV3Limits , DiskImages , \
30- DiskImageInfo , ComputeServers , NetworkLimits , ComputeFlavors , ComputeFlavorItem , Networks , NetworkItem , Subnets , \
31- VMCreateResponse , ComputeServerInfo , ComputeServerActions , ComputeServerActionRebootType , VMKeypairItem , \
32- VMKeypairItemValue , VMKeypairs , LoginResponse , Token , APIProjects
33- from openstack_cli .modules .openstack .objects import OpenStackEndpoints , EndpointTypes , OpenStackQuotas , ImageStatus , \
34- OpenStackUsers , OpenStackVM , OpenStackVMInfo , OSImageInfo , OSFlavor , OSNetwork , VMCreateBuilder , ServerPowerState , \
35- ServerState , AuthRequestBuilder , AuthRequestType , OpenStackQuotaType
31+ from openstack_cli .modules .openstack .api_objects import APIProjects , ComputeFlavorItem , ComputeFlavors , ComputeLimits , \
32+ ComputeServerActionRebootType , ComputeServerActions , ComputeServerInfo , ComputeServers , DiskImageInfo , DiskImages , \
33+ LoginResponse , NetworkItem , NetworkLimits , Networks , Subnets , Token , VMCreateResponse , VMKeypairItem , \
34+ VMKeypairItemValue , VMKeypairs , VolumeV3Limits
35+ from openstack_cli .modules .openstack .objects import AuthRequestBuilder , AuthRequestType , EndpointTypes , ImageStatus , \
36+ OSFlavor , OSImageInfo , OSNetwork , OpenStackEndpoints , OpenStackQuotaType , OpenStackQuotas , OpenStackUsers , \
37+ OpenStackVM , OpenStackVMInfo , ServerPowerState , ServerState , VMCreateBuilder
3638
3739T = TypeVar ('T' )
3840
@@ -97,27 +99,7 @@ def __get_local_cache(self, cache_type: LocalCacheType) -> T:
9799 return self .__local_cache [cache_type .value ]
98100
99101 def __init_after_auth__ (self ):
100- # getting initial data
101- if self ._conf .cache .exists (DiskImageInfo ):
102- self .__cache_images = {k : DiskImageInfo (serialized_obj = v ) for k , v in json .loads (self ._conf .cache .get (DiskImageInfo )).items ()}
103- else :
104- self .images
105-
106- if self ._conf .cache .exists (OSFlavor ):
107- self .__flavors_cache = {k : OSFlavor (serialized_obj = v ) for k , v in json .loads (self ._conf .cache .get (OSFlavor )).items ()}
108- else :
109- self .flavors
110-
111- if self ._conf .cache .exists (OSNetwork ):
112- self .__networks_cache = OSNetwork (serialized_obj = self ._conf .cache .get (OSNetwork ))
113- else :
114- self .networks
115-
116- if not self .__users_cache :
117- self .users
118-
119- # fake cache, used to re-sync server keys from time to time
120- if not self ._conf .cache .exists (VMKeypairItemValue ):
102+ def __cache_ssh_keys ():
121103 conf_keys_hashes = [hash (k ) for k in self ._conf .get_keys ()]
122104 server_keys = self .get_keypairs ()
123105 for server_key in server_keys :
@@ -128,8 +110,57 @@ def __init_after_auth__(self):
128110 print (f"Key { server_key .name } is present locally but have wrong hash, replacing with server key" )
129111 self ._conf .delete_key (server_key .name )
130112 self ._conf .add_key (server_key )
113+
131114 self ._conf .cache .set (VMKeypairItemValue , "this super cache" )
132115
116+ def __cached_network ():
117+ self .__networks_cache = OSNetwork (serialized_obj = self ._conf .cache .get (OSNetwork ))
118+
119+ def __cached_images ():
120+ self .__cache_images = {k : DiskImageInfo (serialized_obj = v ) for k , v in json .loads (self ._conf .cache .get (DiskImageInfo )).items ()}
121+
122+ def __cached_flavors ():
123+ self .__flavors_cache = {k : OSFlavor (serialized_obj = v ) for k , v in json .loads (self ._conf .cache .get (OSFlavor )).items ()}
124+
125+ def __cached_ssh_keys ():
126+ return True
127+
128+ _cached_objects = {
129+ DiskImageInfo : {
130+ False : lambda : self .images ,
131+ True : lambda : __cached_images ()
132+ },
133+ OSFlavor : {
134+ False : lambda : self .flavors ,
135+ True : lambda : __cached_flavors ()
136+ },
137+ OSNetwork : {
138+ False : lambda : self .networks ,
139+ True : lambda : __cached_network ()
140+ },
141+ VMKeypairItemValue : {
142+ False : lambda : __cache_ssh_keys (),
143+ True : lambda : __cached_ssh_keys ()
144+ }
145+ }
146+
147+ need_recache : bool = False in [self ._conf .cache .exists (obj ) for obj in _cached_objects ]
148+ if need_recache and not self .__debug :
149+ p = ProgressBar ("Syncing to the server data" ,20 ,
150+ ProgressBarOptions (CharacterStyles .simple , ProgressBarFormat .PROGRESS_FORMAT_STATUS )
151+ )
152+ p .start (len (_cached_objects ))
153+ for cache_item , funcs in _cached_objects .items ():
154+ p .progress_inc (1 , cache_item .__name__ )
155+ funcs [self ._conf .cache .exists (cache_item )]()
156+ p .stop (hide_progress = True )
157+ else :
158+ for cache_item , funcs in _cached_objects .items ():
159+ funcs [self ._conf .cache .exists (cache_item )]()
160+
161+ if not self .__users_cache :
162+ self .users
163+
133164 def __check_token (self ) -> bool :
134165 headers = {
135166 "X-Auth-Token" : self ._conf .auth_token ,
@@ -176,7 +207,8 @@ def __auth(self, _type: AuthRequestType = AuthRequestType.SCOPED) -> bool:
176207
177208 if not r :
178209 from openstack_cli .core .output import Console
179- Console .print_error ("Authentication server is not accessible: " + r .raw )
210+ data = r .raw if r else "none"
211+ Console .print_error (f"Authentication server is not accessible: { data } " )
180212 return False
181213
182214 if r .code not in [200 , 201 ]:
0 commit comments