@@ -69,42 +69,42 @@ func DescribeNetwork(name string) (any, error) {
6969}
7070
7171// DeleteNetwork deletes a network by name via CloudStack API.
72- func DeleteNetwork (name string ) error {
72+ func DeleteNetwork (name string ) ( string , error ) {
7373 client , err := cloudstack .NewClient ()
7474 if err != nil {
75- return fmt .Errorf ("failed to create CloudStack client: %w" , err )
75+ return "" , fmt .Errorf ("failed to create CloudStack client: %w" , err )
7676 }
7777 params := client .Network .NewListNetworksParams ()
7878 params .SetName (name )
7979 resp , err := client .Network .ListNetworks (params )
8080 if err != nil {
81- return fmt .Errorf ("cloudstack API error: %w" , err )
81+ return "" , fmt .Errorf ("cloudstack API error: %w" , err )
8282 }
8383 if resp == nil || len (resp .Networks ) == 0 {
84- return fmt .Errorf ("network %s not found" , name )
84+ return "" , fmt .Errorf ("network %s not found" , name )
8585 }
8686 nid := resp .Networks [0 ].Id
8787 delp := client .Network .NewDeleteNetworkParams (nid )
8888 if _ , err := client .Network .DeleteNetwork (delp ); err != nil {
89- return fmt .Errorf ("failed to delete network %s: %w" , name , err )
89+ return "" , fmt .Errorf ("failed to delete network %s: %w" , name , err )
9090 }
9191 log .Printf ("Network %s deleted from CloudStack (id=%s)" , name , nid )
92- return nil
92+ return nid , nil
9393}
9494
9595// ApplyNetwork applies or updates a Network resource using the CloudStack API.
9696// It searches for an existing network by name; if none is found, it creates one.
9797// If an existing network is found, it updates the description when it differs
9898// from the desired spec.
99- func ApplyNetwork (netRes * v1.Network ) error {
99+ func ApplyNetwork (netRes * v1.Network ) ( string , error ) {
100100 name := netRes .Metadata .Name
101101 if name == "" {
102- return fmt .Errorf ("network metadata.name is required" )
102+ return "" , fmt .Errorf ("network metadata.name is required" )
103103 }
104104
105105 client , err := cloudstack .NewClient ()
106106 if err != nil {
107- return fmt .Errorf ("failed to create CloudStack client: %w" , err )
107+ return "" , fmt .Errorf ("failed to create CloudStack client: %w" , err )
108108 }
109109
110110 // Try to find existing network by name
@@ -113,23 +113,23 @@ func ApplyNetwork(netRes *v1.Network) error {
113113 listParams .SetListall (true )
114114 listResp , err := client .Network .ListNetworks (listParams )
115115 if err != nil {
116- return fmt .Errorf ("failed to list networks: %w" , err )
116+ return "" , fmt .Errorf ("failed to list networks: %w" , err )
117117 }
118118
119119 // Not found -> create
120120 if listResp == nil || len (listResp .Networks ) == 0 {
121121 if netRes .Spec .NetworkOffering == "" || netRes .Spec .Zone == "" {
122- return fmt .Errorf ("network create requires spec.networkOffering and spec.zone in standalone mode" )
122+ return "" , fmt .Errorf ("network create requires spec.networkOffering and spec.zone in standalone mode" )
123123 }
124124 // Resolve zone name to ID; require resolution or return an error.
125125 zoneID , zerr := ResolveZone (netRes .Spec .Zone )
126126 if zerr != nil {
127- return fmt .Errorf ("failed to resolve zone %s: %w" , netRes .Spec .Zone , zerr )
127+ return "" , fmt .Errorf ("failed to resolve zone %s: %w" , netRes .Spec .Zone , zerr )
128128 }
129129 // Resolve network offering name to ID; require resolution.
130130 offeringID , offErr := ResolveNetworkOffering (netRes .Spec .NetworkOffering )
131131 if offErr != nil {
132- return fmt .Errorf ("failed to resolve network offering %s: %w" , netRes .Spec .NetworkOffering , offErr )
132+ return "" , fmt .Errorf ("failed to resolve network offering %s: %w" , netRes .Spec .NetworkOffering , offErr )
133133 }
134134 createParams := client .Network .NewCreateNetworkParams (name , offeringID , zoneID )
135135 if netRes .Spec .Description != "" {
@@ -182,12 +182,12 @@ func ApplyNetwork(netRes *v1.Network) error {
182182 }
183183 resp , err := client .Network .CreateNetwork (createParams )
184184 if err != nil {
185- return fmt .Errorf ("cloudstack create network error: %w" , err )
185+ return "" , fmt .Errorf ("cloudstack create network error: %w" , err )
186186 }
187187 log .Printf ("Created Network %s (id=%s)" , name , resp .Id )
188- return nil
188+ return resp . Id , nil
189189 }
190190 // Resource exists — updates are not supported at this time
191191 existing := listResp .Networks [0 ]
192- return fmt .Errorf ("network %s already exists in CloudStack (id=%s); updates are not supported" , name , existing .Id )
192+ return "" , fmt .Errorf ("network %s already exists in CloudStack (id=%s); updates are not supported" , name , existing .Id )
193193}
0 commit comments