Skip to main content

unitycatalog_common/models/resources/
mod.rs

1pub use super::_gen::ObjectLabel;
2pub use olai_store::{EMPTY_RESOURCE_NAME, ResourceName, ResourceRef};
3
4// Re-export the resource_name! macro from name.rs (which wraps the derive macro)
5pub use name::resource_name;
6
7mod name;
8
9/// Resource that a policy can authorize.
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub enum ResourceIdent {
12    Agent(ResourceRef),
13    AgentSkill(ResourceRef),
14    Share(ResourceRef),
15    Credential(ResourceRef),
16    ExternalLocation(ResourceRef),
17    Catalog(ResourceRef),
18    Schema(ResourceRef),
19    Table(ResourceRef),
20    Recipient(ResourceRef),
21    Provider(ResourceRef),
22    Column(ResourceRef),
23    Volume(ResourceRef),
24    Function(ResourceRef),
25    TagPolicy(ResourceRef),
26    StagingTable(ResourceRef),
27    PolicyInfo(ResourceRef),
28}
29
30impl ResourceIdent {
31    pub fn label(&self) -> &ObjectLabel {
32        self.as_ref()
33    }
34
35    pub fn reference(&self) -> &ResourceRef {
36        self.as_ref()
37    }
38
39    pub fn agent(name: impl Into<ResourceRef>) -> Self {
40        Self::Agent(name.into())
41    }
42
43    pub fn agent_skill(name: impl Into<ResourceRef>) -> Self {
44        Self::AgentSkill(name.into())
45    }
46
47    pub fn share(name: impl Into<ResourceRef>) -> Self {
48        Self::Share(name.into())
49    }
50
51    pub fn credential(name: impl Into<ResourceRef>) -> Self {
52        Self::Credential(name.into())
53    }
54
55    pub fn catalog(name: impl Into<ResourceRef>) -> Self {
56        Self::Catalog(name.into())
57    }
58
59    pub fn schema(name: impl Into<ResourceRef>) -> Self {
60        Self::Schema(name.into())
61    }
62
63    pub fn table(name: impl Into<ResourceRef>) -> Self {
64        Self::Table(name.into())
65    }
66
67    pub fn column(name: impl Into<ResourceRef>) -> Self {
68        Self::Column(name.into())
69    }
70
71    pub fn external_location(name: impl Into<ResourceRef>) -> Self {
72        Self::ExternalLocation(name.into())
73    }
74
75    pub fn recipient(name: impl Into<ResourceRef>) -> Self {
76        Self::Recipient(name.into())
77    }
78
79    pub fn provider(name: impl Into<ResourceRef>) -> Self {
80        Self::Provider(name.into())
81    }
82
83    pub fn volume(name: impl Into<ResourceRef>) -> Self {
84        Self::Volume(name.into())
85    }
86
87    pub fn function(name: impl Into<ResourceRef>) -> Self {
88        Self::Function(name.into())
89    }
90
91    pub fn tag_policy(name: impl Into<ResourceRef>) -> Self {
92        Self::TagPolicy(name.into())
93    }
94
95    pub fn staging_table(name: impl Into<ResourceRef>) -> Self {
96        Self::StagingTable(name.into())
97    }
98
99    pub fn policy_info(name: impl Into<ResourceRef>) -> Self {
100        Self::PolicyInfo(name.into())
101    }
102}
103
104impl std::fmt::Display for ResourceIdent {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        match self {
107            ResourceIdent::Agent(r) => write!(f, "agent:{}", r),
108            ResourceIdent::AgentSkill(r) => write!(f, "agent_skill:{}", r),
109            ResourceIdent::Share(r) => write!(f, "share:{}", r),
110            ResourceIdent::Credential(r) => write!(f, "credential:{}", r),
111            ResourceIdent::ExternalLocation(r) => write!(f, "external_location:{}", r),
112            ResourceIdent::Catalog(r) => write!(f, "catalog:{}", r),
113            ResourceIdent::Schema(r) => write!(f, "schema:{}", r),
114            ResourceIdent::Table(r) => write!(f, "table:{}", r),
115            ResourceIdent::Recipient(r) => write!(f, "recipient:{}", r),
116            ResourceIdent::Provider(r) => write!(f, "provider:{}", r),
117            ResourceIdent::Column(r) => write!(f, "column:{}", r),
118            ResourceIdent::Volume(r) => write!(f, "volume:{}", r),
119            ResourceIdent::Function(r) => write!(f, "function:{}", r),
120            ResourceIdent::TagPolicy(r) => write!(f, "tag_policy:{}", r),
121            ResourceIdent::StagingTable(r) => write!(f, "staging_table:{}", r),
122            ResourceIdent::PolicyInfo(r) => write!(f, "policy_info:{}", r),
123        }
124    }
125}
126
127impl AsRef<ResourceRef> for ResourceIdent {
128    fn as_ref(&self) -> &ResourceRef {
129        match self {
130            ResourceIdent::Agent(r) => r,
131            ResourceIdent::AgentSkill(r) => r,
132            ResourceIdent::Share(r) => r,
133            ResourceIdent::Credential(r) => r,
134            ResourceIdent::ExternalLocation(r) => r,
135            ResourceIdent::Catalog(r) => r,
136            ResourceIdent::Schema(r) => r,
137            ResourceIdent::Table(r) => r,
138            ResourceIdent::Recipient(r) => r,
139            ResourceIdent::Provider(r) => r,
140            ResourceIdent::Column(r) => r,
141            ResourceIdent::Volume(r) => r,
142            ResourceIdent::Function(r) => r,
143            ResourceIdent::TagPolicy(r) => r,
144            ResourceIdent::StagingTable(r) => r,
145            ResourceIdent::PolicyInfo(r) => r,
146        }
147    }
148}
149
150impl AsRef<ObjectLabel> for ResourceIdent {
151    fn as_ref(&self) -> &ObjectLabel {
152        match self {
153            ResourceIdent::Agent(_) => &ObjectLabel::Agent,
154            ResourceIdent::AgentSkill(_) => &ObjectLabel::AgentSkill,
155            ResourceIdent::Share(_) => &ObjectLabel::Share,
156            ResourceIdent::Credential(_) => &ObjectLabel::Credential,
157            ResourceIdent::ExternalLocation(_) => &ObjectLabel::ExternalLocation,
158            ResourceIdent::Catalog(_) => &ObjectLabel::Catalog,
159            ResourceIdent::Schema(_) => &ObjectLabel::Schema,
160            ResourceIdent::Table(_) => &ObjectLabel::Table,
161            ResourceIdent::Recipient(_) => &ObjectLabel::Recipient,
162            ResourceIdent::Provider(_) => &ObjectLabel::Provider,
163            ResourceIdent::Column(_) => &ObjectLabel::Column,
164            ResourceIdent::Volume(_) => &ObjectLabel::Volume,
165            ResourceIdent::Function(_) => &ObjectLabel::Function,
166            ResourceIdent::TagPolicy(_) => &ObjectLabel::TagPolicy,
167            ResourceIdent::StagingTable(_) => &ObjectLabel::StagingTable,
168            ResourceIdent::PolicyInfo(_) => &ObjectLabel::PolicyInfo,
169        }
170    }
171}
172
173impl From<ResourceIdent> for ResourceRef {
174    fn from(ident: ResourceIdent) -> Self {
175        match ident {
176            ResourceIdent::Agent(r) => r,
177            ResourceIdent::AgentSkill(r) => r,
178            ResourceIdent::Share(r) => r,
179            ResourceIdent::Credential(r) => r,
180            ResourceIdent::ExternalLocation(r) => r,
181            ResourceIdent::Catalog(r) => r,
182            ResourceIdent::Schema(r) => r,
183            ResourceIdent::Table(r) => r,
184            ResourceIdent::Recipient(r) => r,
185            ResourceIdent::Provider(r) => r,
186            ResourceIdent::Column(r) => r,
187            ResourceIdent::Volume(r) => r,
188            ResourceIdent::Function(r) => r,
189            ResourceIdent::TagPolicy(r) => r,
190            ResourceIdent::StagingTable(r) => r,
191            ResourceIdent::PolicyInfo(r) => r,
192        }
193    }
194}
195
196impl From<&ResourceIdent> for ResourceRef {
197    fn from(ident: &ResourceIdent) -> Self {
198        (ident as &dyn AsRef<ResourceRef>).as_ref().clone()
199    }
200}
201
202impl From<&ResourceIdent> for ObjectLabel {
203    fn from(ident: &ResourceIdent) -> Self {
204        *(ident as &dyn AsRef<ObjectLabel>).as_ref()
205    }
206}
207
208impl From<ResourceIdent> for ObjectLabel {
209    fn from(ident: ResourceIdent) -> Self {
210        (&ident).into()
211    }
212}
213
214pub trait ResourceExt {
215    /// Get the name of the resource
216    fn resource_name(&self) -> ResourceName;
217
218    /// Get the reference for the resource
219    ///
220    /// Depending on the resource type, this may be a UUID or a name.
221    /// If possible, implementations should prefer to use the UUID
222    /// as it is globally unique. However not all resource-like objects
223    /// have a UUID field, or the UUID field may be optional.
224    fn resource_ref(&self) -> ResourceRef;
225
226    /// Get the ident for the resource
227    fn resource_ident(&self) -> ResourceIdent;
228}
229
230impl<T: ResourceExt> From<&T> for ResourceIdent {
231    fn from(resource: &T) -> Self {
232        resource.resource_ident()
233    }
234}
235
236impl ObjectLabel {
237    pub fn to_ident(&self, id: impl Into<ResourceRef>) -> ResourceIdent {
238        match self {
239            ObjectLabel::Agent => ResourceIdent::agent(id),
240            ObjectLabel::AgentSkill => ResourceIdent::agent_skill(id),
241            ObjectLabel::Share => ResourceIdent::share(id),
242            ObjectLabel::Credential => ResourceIdent::credential(id),
243            ObjectLabel::Catalog => ResourceIdent::catalog(id),
244            ObjectLabel::Schema => ResourceIdent::schema(id),
245            ObjectLabel::Table => ResourceIdent::table(id),
246            ObjectLabel::ExternalLocation => ResourceIdent::external_location(id),
247            ObjectLabel::Recipient => ResourceIdent::recipient(id),
248            ObjectLabel::Provider => ResourceIdent::provider(id),
249            ObjectLabel::Column => ResourceIdent::column(id),
250            ObjectLabel::Volume => ResourceIdent::volume(id),
251            ObjectLabel::Function => ResourceIdent::function(id),
252            ObjectLabel::TagPolicy => ResourceIdent::tag_policy(id),
253            ObjectLabel::StagingTable => ResourceIdent::staging_table(id),
254            ObjectLabel::PolicyInfo => ResourceIdent::policy_info(id),
255        }
256    }
257}